Regex asterisk usage

依然范特西╮ 提交于 2021-02-05 10:48:07

问题


According to MDN x* : *Matches the preceding item x 0 or more times.*

Essentially the preceding characters should be completely optional. The string will be matched whether they exist or not. So why is it that:

1.

var text  = "foobar";
var re    = /q*/;
var found = text.match(re);
console.log(found); // prints '["", index: 0, input: "foobar"]'

but

var re    = /qz*/;
console.log(found); // prints 'null'

Both expressions equally don't exist and thus should be matched 0 times and '""' should be returned.

Or:

2.

var re    = /fz*/;
console.log(found); // prints '["f", index: 0, input: "foobar"]'

but

var re    = /fzq*/;
console.log(found); // prints 'null'

What's happening here? From my understanding 'fzq' doesn't exist so should be matched 0 times, and '""' should be returned, right? And if it somehow matches on per character basis instead of the whole string, 'fzq*' should return the same result as 'fz' - 'f' is matched once and the rest is matched 0 times. But apparently this is not what's happening.

Could someone shed some light on what the hell is happening here?


回答1:


x* : Matches the preceding item x 0 or more times.

You misunderstood this statement with everything before * is matched zero or more times.

When you match q*, the regex will search for q in the input string. As there is no q in the foobar, it'll return an empty string.

When you try to match qz*, the regex will try to search for any number of zs after q. As there is no q in the input string, it'll return null.

To match more characters by * or any other quantifiers, you need to group them using parenthesis.

(qz)*

which means, match the string qz zero or more times.

To understand a regex, you can use https://regex101.com/ site.



来源:https://stackoverflow.com/questions/44580728/regex-asterisk-usage

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