gpt4 book ai didi

javascript - 如果第一个字符不匹配,regexp exec 内部索引不会进行

转载 作者:行者123 更新时间:2023-11-30 21:06:03 25 4
gpt4 key购买 nike

我需要匹配组中不以“/”开头的数字。

为了做到这一点,我制作了以下正则表达式:

var reg = /(^|[^,\/])([0-9]*\.?[0-9]*)/g;

第一部分匹配字符串的开头和除“/”之外的任何其他内容,第二部分匹配数字。关于正则表达式,一切正常(它符合我的需要)。我用 https://regex101.com/供测试用。此处示例:https://regex101.com/r/7UwEUn/1

问题是,当我在 js(下面的脚本)中使用它时,如果字符串的第一个字符不是数字,它会进入无限循环。仔细观察,它似乎一直在匹配字符串的开头,从未进一步进行。

 var reg = /(^|[^,\/])([0-9]*\.?[0-9]*)/g;
var text = "a 1 b";
while (match = reg.exec(text)) {
if (typeof match[2] != 'undefined' && match[2] != '') {
numbers.push({'index': match.index + match[1].length, 'value': match[2]});
}
}

如果字符串以数字(“1 a b”)开头,则一切正常。

问题似乎出在这里 (^|[^,/]) - 删除 ^|将解决无限循环的问题,但它不会匹配我需要的以数字开头的字符串。

知道为什么内部索引没有进展吗?

最佳答案

无限循环是由于您的正则表达式可以匹配空字符串而引起的。你不太可能需要空字符串(即使根据你的代码判断),所以让它至少匹配一位数字,将最后一个 * 替换为 +:

var reg = /(^|[^,\/])([0-9]*\.?[0-9]+)/g; 
var text = "a 1 b a 2 ana 1/2 are mere (55";
var numbers=[];
while (match = reg.exec(text)) {
numbers.push({'index': match.index + match[1].length, 'value': match[2]});
}
console.log(numbers);

请注意,此正则表达式不会匹配 34. 这样的数字,在这种情况下,您可以使用 /(^|[^,\/])([0-9]*\.?[0-9]+|[0-9]*\.)/g,见this regex demo .

或者,您可以使用另一个“技巧”,在不匹配时手动推进正则表达式 lastIndex:

var reg = /(^|[^,\/])([0-9]*\.?[0-9]+)/g;
var text = "a 1 b a 2 ana 1/2 are mere (55";
var numbers=[];
while (match = reg.exec(text)) {
if (match.index === reg.lastIndex) {
reg.lastIndex++;
}
if (match[2]) numbers.push({'index': match.index + match[1].length, 'value': match[2]});
}
console.log(numbers);

关于javascript - 如果第一个字符不匹配,regexp exec 内部索引不会进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46587762/

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