Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用正则表达式进行各种url验证_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 使用正则表达式进行各种url验证

Javascript 使用正则表达式进行各种url验证,javascript,jquery,regex,Javascript,Jquery,Regex,如何验证以下URL: http:www.google.com/test http:www.google.com www.google.com 谷歌网站 在上述所有情况下,我的正则表达式都将返回true,并且我希望从第一种情况中提取url。我们如何实现此功能?我的观点: var match = url.match(/(http:\/\/www\.google\.com\/test)|http:\/\/(www\.)?google\.com/); if(match){ alert("found

如何验证以下URL:

  • http:www.google.com/test
  • http:www.google.com
  • www.google.com
  • 谷歌网站
  • 在上述所有情况下,我的正则表达式都将返回true,并且我希望从第一种情况中提取url。我们如何实现此功能?

    我的观点:

    var match = url.match(/(http:\/\/www\.google\.com\/test)|http:\/\/(www\.)?google\.com/);
    if(match){
      alert("found it");
      if(match.length>1 && match[1]){
        alert("captured it "+match[1]);
      }
    }
    
    显然,只有
    |
    一侧的捕获组使捕获组存在,但未定义


    PS:我已经在url中添加了斜杠,它们应该在那里吗?

    是否要测试url是否是您提到的四个url之一(如果是,请尝试==)?到目前为止,你尝试了什么?@carlpett,我在他的问题中没有看到PHP标记。我的观点是,regex是url验证的错误工具