Javascript 验证google文档和office 365 url
我使用正则表达式验证提供的url是否有效。为了测试有效性(谷歌文档url或office 365文档),我做了以下操作,但不起作用Javascript 验证google文档和office 365 url,javascript,regex,Javascript,Regex,我使用正则表达式验证提供的url是否有效。为了测试有效性(谷歌文档url或office 365文档),我做了以下操作,但不起作用 var url = "https://hello-my.sharepoint.com/:w:/r/personal/; var urlRegx = new RegExp('^(docs.google.com|(http|https)://[A-Za-z]-.my.sharepoint.com)', 'i'); console.log(urlRegx.test(url)
var url = "https://hello-my.sharepoint.com/:w:/r/personal/;
var urlRegx = new RegExp('^(docs.google.com|(http|https)://[A-Za-z]-.my.sharepoint.com)', 'i');
console.log(urlRegx.test(url));
当我有sharepoint url时,这给了我
false
,但当我有`url=“docs.google.com/document/”时,你在表达式末尾有一个额外的结束括号)
,你应该删除它
在[a-Za-z]
之后,您还缺少一个+
(因为没有加号,您只匹配一个字符)
以下是一个工作示例:
var url=”https://hello-my.sharepoint.com/:w:/r/personal/";
var urlRegx=new RegExp('(docs.google.com |(http | https))(://[A-Za-z]+-my.sharepoint.com)?','i');
log(urlRegx.test(url))代码>尝试var urlRegx=/^(?:docs\.google\.com | https?:\/\/[A-Za-z]+-my\.sharepoint\.com/i代码>。看,你还是没逃过一劫。请参阅console.log('\.')
。在使用RegExp构造函数构建的正则表达式模式中,不需要转义/
,/
不是特殊的正则表达式元字符。@WiktorStribiżew-Ah是的,你说得对。谢谢你指出这一点。我已经更新了我的答案,如果你试图验证“docs.google.com”的url,它会将false作为output@milan尝试newregexp('(docs.google.com |(http | https))(://[A-Za-z]+-my.sharepoint.com)?,'i')代码>取而代之