代码之家  ›  专栏  ›  技术社区  ›  milan

验证Google Docs和Office 365 URL

  •  0
  • milan  · 技术社区  · 6 年前

    我正在使用regex验证提供的URL是否有效。为了测试有效性(google docs url或office 365 docs),我做了以下操作,但没有用

    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));
    

    这是给我的 false 当我有了SharePoint URL,但是 true 当我有了'url=“docs.google.com/document/”

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nick Parsons Felix Kling    6 年前

    你还有一个右括号 ) 在表达式的末尾,应该删除它。

    您还缺少 + 之后 [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');
    console.log(urlRegx.test(url));

    注: 使用regexp构造函数时,不需要转义特殊字符。因此,如果不使用 RegExp 构造函数必须转义特殊字符,如:

    var urlRegx = /(docs\.google\.com|(http|https)):\/\/[A-Za-z]+-my\.sharepoint\.com/i;