My regex successfully validates many URLs except http://www.google
Here's my URL validator in JSFiddle: http://jsfiddle.net/z23nZ/2/
It correctly validates the following URLs:
http://www.google.com
gives True
www.google.com
gives True
http://www.rootsweb.ancestry.com/~mopoc/links.htm
gives True
http:// www.
gives False
...but not this one:
http://www.google
gives True
It's not correct to return true in this case. How can I validate that case?
I think you need to way simplify this. There are plenty of URL validation RegExes out there, but as an exercise, I'll go through my thought process for constructing one.
/((http|ftp)s?:\\/\\/)?
\\S+
[\\.\\/]\\S*/
Now put it all together:
/(((http|ftp)s?:\/\/)|(\S+[\.\/]))\S*[^\s\.]*/
I'm guessing that your attempting to look for www.google
is because of the new TLDs... the fact is, such URLs might just look like google
, and so any word could be a URL. Trying to come up with a catch-all regex which matches valid URLs and nothing else isn't possible, so you're best just going with something simple like the above.
Edit: I've stuck a |
in there between the protocol part and the non-whitespace-then-dot-or-slash part to match http://google
if people choose to write new URLs like that
Edit 2: See comments for the next improvement. It makes sure google.com
matches, http://google
matches, and even google/
matches, but not a.
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.