问题描述
这是代码部分:
var
[...snip...]
ye=/^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i,
此正则表达式使用两次,两次均使用ye.test(a)
.但是,我发现没有不匹配的字符串.我觉得很难相信,但是RegExp
真的匹配每个可以想象的字符串吗?
演示:
var ye = /^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;
console.log(ye.test("askjvhlkauehavkn"))
console.log(ye.test("/"))
console.log(ye.test("https:"))
console.log(ye.test("mailto/L:"))
(?:https?|mailto|ftp)
匹配http
或https
或mailto
或ftp
:|[^:/?#]*
,这是替代方法::
或除:/>#
之外的任何其他字符,零次或更多次,然后是(?:[/?#]|$)
,这表示/?#
之一或字符串的结尾($
). /p>
它将匹配mailto:
,ftp:
,https:
,ftpasda
(以ftp
,https
,http
,mailto
开头的任何字符串,后跟一个冒号或任何其他数字)但:/>#
).
更新
检查后,发现非捕获组之外的交替不仅适用于结肠,也适用于整个组.因此,如果mailto
或替换中的任何字符串不匹配,则正则表达式引擎将在提及的替换的另一侧尝试匹配模式.这是不匹配的字符串的示例::///////
. 演示.
Here's the section of code:
var
[...snip...]
ye=/^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i,
This regular expression is used twice, both times with ye.test(a)
. And yet, I've found no strings that it doesn't match. I find that hard to believe, butdoes this RegExp
really match every string imaginable?
Demonstration:
var ye = /^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;
console.log(ye.test("askjvhlkauehavkn"))
console.log(ye.test("/"))
console.log(ye.test("https:"))
console.log(ye.test("mailto/L:"))
(?:https?|mailto|ftp)
matches http
or https
or mailto
or ftp
followed by:|[^:/?#]*
, which is alternative: :
or anything but :/>#
, zero or more times, and then followed by (?:[/?#]|$)
, which means one of /?#
or end of the string ($
).
It will match mailto:
, ftp:
, https:
, ftpasda
(any string starting with ftp
, https
, http
, mailto
followed by a colon or any number of anything but :/>#
).
UPDATE
After checking, it occurs that that alternation outside the non-capturing group applies not only to a colon, but also to whole group as well. So, if mailto
or any string in the alternation doesn't match, regex engine will try matching pattern on the other side of mentioned alternation. This is example of string that won't match: :///////
. Demo.
这篇关于Google Analytics(分析)的RegExp确实有作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!