我需要在'id-odes ='后捕获数字,但只有没有这个短语的数字。我写了这样的东西
"id-odes=50388635:id-odes=503813535:id-odes=50334635"
.match(/(?:id-odes=)([0-9]*)/g);
但它返回
["id-odes=50388635", "id-odes=503813535", "id-odes=50334635"]
而不是
[50388635, 503813535, 50334635]
请帮助解释为什么我的方式无法正常工作。 感谢
答案 0 :(得分:4)
您可以迭代结果,而不仅仅是输出数组:
var re =/id-odes=([0-9]*)/g,
s = "id-odes=50388635:id-odes=503813535:id-odes=50334635";
while ((match = re.exec(s)) !== null) {
console.log(match[1]);
}
答案 1 :(得分:0)
如果你想迭代匹配,那么你可以使用类似的东西:
s = "id-odes=50388635:id-odes=503813535:id-odes=50334635"
re = /(?:id-odes=)([0-9]*)/
while (match = re.exec(s))
{
console.log(match[1]); // This is the number part
}
假设整个字符串都是这种格式,你当然可以使用
"id-odes=50388635:id-odes=503813535:id-odes=50334635".match(/[0-9]+/g)
但如果字符串中有任何其他数字,那当然会中断。
解释为什么.match(/(?:id-odes=)([0-9]*)/g);
给出了错误的结果非常简单:无论捕获组如何,都可以获得正则表达式匹配的所有内容。