Does this RegExp from Google Analytics actually do anything?

随声附和 提交于 2019-12-11 15:42:34

问题


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, but does 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:"))

回答1:


(?: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.



来源:https://stackoverflow.com/questions/52689595/does-this-regexp-from-google-analytics-actually-do-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!