简体   繁体   中英

How to validate my URL efficienty using JavaScript?

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.

  1. First, you need to match a protocol if there is one: /((http|ftp)s?:\\/\\/)?
  2. Then match any series of non-whitespace characters: \\S+
  3. If you're trying to pick out URLs from text, you'll want to look for signs that it is a URL. Look for dots or slashes, then more non-whitespace: [\\.\\/]\\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM