gpt4 book ai didi

javascript - while 循环中 regex.exec() 赋值的更好解决方案

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:28 26 4
gpt4 key购买 nike

这里有更好的解决方案吗?我尽量避免在 while 中进行赋值,但仍然能够遍历匹配项并使用捕获的组。

var match = "";
var CSS_URL_PATTERN = /url\s*\(\s*["|']?(.*?)\s*["|']?\)\s*/gm
while ((match = CSS_URL_PATTERN.exec(someBigCSSString)) !== null) {
// Do stuff here per match…
}

我为这个问题添加了更多上下文,还有一个 RegEx 示例。

最佳答案

当我需要.exec时,我总是这样做:

var re = /.../g, match;
while (match = re.exec(...)) {
//...
}

带有 g 标志的正则表达式在循环条件中会产生无限效果。

What are the differences between Fx 3.x and Fx 4.x that made many userscripts stop working?

关于javascript - while 循环中 regex.exec() 赋值的更好解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com