这个正则表达式验证了什么?什么是匹配和不匹配的例子?

时间:2013-11-27 17:17:05

标签: regex url-validation

这是从验证器npm模块中删除的URL验证器。我是regex的新手,有人介意帮我解码吗?或许可以提到网址的匹配与不匹配?

(/^(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:\/[^\s]*)?$/i)

3 个答案:

答案 0 :(得分:1)

这是一个URL验证器,旨在捕获尽可能多的文档化URL(和IP地址)标准。

我建议将其添加到regex101.com以了解它并测试各种匹配。

以下是您可以使用的示例 - 只需更改测试字符串中的url即可查看结果。 http://regex101.com/r/jQ1lZ5

它匹配的一些例子:

有些不匹配的内容:

  • wwww..google.com
  • HTTP :: //www.google.com
  • 12.20.140.256(不是有效的IP地址!)
  • local_host

答案 1 :(得分:0)

你当然必须阅读有关regexp的内容。无论如何,这里有一些提示(非详尽无遗):

^ : out of a class it matches the beginning of a string
^ : in a class it makes your class a negation class
$ : matches the end of a string
() : creates group which could be reusable, quantifiable, etc.
? : tells the last character or group can be present once or not present
+ : tells the last character or group is present at least once
* : tells the last character or group can be present once or more or not present
\ : escape the following char. Usefull is you have to match a character which is also a metacharacter 
| : is for alternative 
{x, y} : allow you to quantify more precisely than *, ? or +
[] : allow you to create class. [abc] is equivalent to a|b|c
? : after a + or * make it non greedy. By default quantifier are greedy (match the langest string possible)

答案 2 :(得分:-1)

在计算中,正则表达式(缩写为regex或regexp)是形成搜索模式的字符序列,主要用于与字符串进行模式匹配,或字符串匹配,即“查找和替换”类操作。这个概念出现在20世纪50年代,当时美国数学家Stephen Kleene将常规语言的描述形式化,并与Unix文本处理实用程序ed,编辑器和grep(全局正则表达式打印)(过滤器)共同使用。 / p>

http://en.wikipedia.org/wiki/Regular_expression