1

我做错了什么,还是谷歌浏览器的错?
使用非捕获组和捕获组与不使用它们的效果相同。

RegExr 显示第一个预期结果。http://regexr.com?30mjo

var text = 'startdate: 123456, enddate: 789012';
var unix = text.match(/(?:start|end)date: (\d+)/g);
console.log(unix);

实际结果

["startdate: 123456", "enddate: 789012"]

预期结果

["123456", "789012"] or  
["startdate: 123456", "123456", "enddate: 789012", "789012"]
4

2 回答 2

1

看起来规范说它应该以这种方式工作。

相关线路是

4. Let matchStr be the result of calling the [[Get]] internal method of result with argument "0"

在ecmascript 规范的第146 页上,result您从调用exec.

除了手动调用 exec 并像这样收集结果之外,我一直无法找到一种方法来完成这项工作:

var regex = /(?:start|end)date: (\d+)/g;
var text = 'startdate: 123456, enddate: 789012';

var result;
var unix = [];

while(result = regex.exec(text)){    
    unix.push(result[1]);
}

console.log(unix);
于 2012-04-18T22:14:34.880 回答
-1

你做错了什么。

那这个呢?

/((?:start|end)date: (\d+))/g
于 2012-04-18T21:59:09.753 回答