various url validation using regular expression
How can I validate the following urls:
- http:www.google.com/test
- http:www.google.com
- www.google.com
- 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?
精彩评论