简体   繁体   English

具有全局多行无限匹配的RegExp

[英]RegExp with global multiline infinite matches

Ok so i was trying to match all lines and obtain the string index of the matches but i ran into a weird issue 好的,所以我试图匹配所有的行,并获得匹配的字符串索引,但我遇到了一个奇怪的问题

var string = 'a\n',
    regexp = /^.*$/gim;

if you run 如果你跑

regexp.exec(string);

It will eventually match "" infinitely, why? 它最终将无限匹配,为什么? The reason i ask is because its common for people to perform these type of matches in a while loop and this will produce an infinite loop unless you are ensuring that the previous match index is not the current index. 我问的原因是因为人们在while循环中执行这些类型的匹配是常见的,除非你确保前一个匹配索引不是当前索引,否则这将产生一个无限循环。

Edit so the solution to my issue required me doing 编辑所以解决我的问题需要我做

while (match = regexp.exec(string)) {
    if (!match[0].length) {
        regexp.lastIndex++;
    }
}

Because .* matches a zero-length string. 因为.*匹配零长度字符串。 At the end of the string, it can always match this again. 在字符串的末尾,它总是可以再次匹配。

It's matching the end of the string. 它匹配字符串的结尾。 When the m flag is used, ^ equals the start of a line... and you got a new line at the end, and the lastIndex (the value that's used to start the search from) won't move from that, because it matched an empty string. 当使用m标志时, ^等于一行的开始...并且你在结尾处有一个新行,而lastIndex (用于开始搜索的值)将不会从那里移动,因为它匹配一个空字符串。

To better understand this, try using this: 为了更好地理解这一点,请尝试使用:

var string = "a\n\n\b\n";

It will never match "b" either. 它永远不会匹配“b”。

Your string variable has 2 NEWLINE characters, resulting in a 3 line string 您的字符串变量有2个NEWLINE字符,从而产生3行字符串

a
b
<<an empty line>>

Your RegEx states that you're searching for strings that matches entire lines, .* means any character 0 or more times 您的RegEx表示您正在搜索与整行匹配的字符串, .*表示任何字符为0或更多次

Since an empty line matches that .* Regex, you'll end up with the empty string as result. 由于空行匹配.* Regex,因此结果将以空字符串结束。

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

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