开发者

various url validation using regular expression

How can I validate the following urls:

  1. http:www.google.com/test
  2. http:www.google.com
  3. www.google.com
  4. google.com

In all the above cases, my regular expression will return true and also I want to extract the url from the first case.How we cam impl开发者_如何学Pythonement this feature?


My stab at it:

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

Apparently, a capturing group in only one side of the | makes the capturing group to be there but undefined

PS: I have added slashes to the URLs, shouldn't they be there?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜