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:
I’m guessing that your attempting to look for
www.googleis because of the new TLDs… the fact is, such URLs might just look likegoogle, 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 matchhttp://googleif people choose to write new URLs like thatEdit 2: See comments for the next improvement. It makes sure
google.commatches,http://googlematches, and evengoogle/matches, but nota..