简体   繁体   English

如何使用JavaScript有效地验证我的URL?

[英]How to validate my URL efficienty using JavaScript?

My regex successfully validates many URLs except http://www.google 我的正则表达式成功验证了除http://www.google之外的许多网址

Here's my URL validator in JSFiddle: http://jsfiddle.net/z23nZ/2/ 这是我在JSFiddle中的URL验证器: http//jsfiddle.net/z23nZ/2/

It correctly validates the following URLs: 它正确验证了以下URL:

http://www.google.com gives True http://www.google.com给出了True

www.google.com gives True www.google.com给出了真实

http://www.rootsweb.ancestry.com/~mopoc/links.htm gives True http://www.rootsweb.ancestry.com/~mopoc/links.htm给出了True

http:// www. gives False 假的

...but not this one: ......但不是这个:

http://www.google gives True http://www.google给予True

It's not correct to return true in this case. 在这种情况下返回true是不正确的。 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. 那里有很多URL验证RegExes,但作为练习,我将完成构建一个的思考过程。

  1. First, you need to match a protocol if there is one: /((http|ftp)s?:\\/\\/)? 首先,你需要匹配一个协议,如果有一个: /((http|ftp)s?:\\/\\/)?
  2. Then match any series of non-whitespace characters: \\S+ 然后匹配任何系列的非空白字符: \\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*/ 寻找点或斜线,然后是更多的非空白: [\\.\\/]\\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. 我猜你试图寻找www.google是因为新TLD ...事实上,这样的网址可能看起来像google ,所以任何单词都可能是一个网址。 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. 试图提出一个匹配有效URL的全能正则表达式是不可能的,所以你最好只使用像上面那样简单的东西。

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 如果人们选择写这样的新URL,那么在协议部分和非空白 - 然后 - 点 - 或 - 斜杠部分之间匹配http://google

Edit 2: See comments for the next improvement. 编辑2:查看评论以获得下一个改进。 It makes sure google.com matches, http://google matches, and even google/ matches, but not a. 它确保google.com匹配, http://google匹配,甚至google/匹配,但不是a. .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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