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

时间:2011-08-30 07:42:51

标签: javascript jquery regex

如何验证以下网址:

  1. HTTP:www.google.com/test
  2. HTTP:www.google.com
  3. www.google.com
  4. google.com
  5. 在上述所有情况下,我的正则表达式将返回true,我也想从第一个案例中提取url。我们如何实现此功能?

1 个答案:

答案 0 :(得分:0)

我刺伤了它:

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:我在网址上添加了斜杠,不应该在那里吗?

相关问题