更新时间:2023-11-26 16:15:22
您正在使用不包含/g
(全局修饰符)的正则表达式运行循环.这意味着,每次您的正则表达式找到匹配项时,lastIndex
属性都不会更改,并且在下一次迭代期间,将找到相同的匹配项,依此类推,以此类推,从而导致无限循环.
You are running a loop with a regex that does not contain a /g
(global modifier). That means, that each time your regex finds a match, the lastIndex
property will not change, and during the next iteration, the same match will be found, and so on and so forth, causing an infinite loop.
请参阅MDN上的 String#exec
文档:
See String#exec
documentation at MDN:
lastIndex
下一场比赛开始的索引.如果不存在"g",则该值将保持为0.
lastIndex
The index at which to start the next match. When "g" is absent, this will remain as 0.
使用
let reg = /\([^)]+\)/g;
它将匹配(
,然后匹配除)
之外的1个以上字符,然后匹配一个)
.
It will match all occurrences of (
, then 1+ chars other than )
and then a )
.