我不知道如何从以下示例中提取多个匹配项:
这段代码:
/prefix-(\w+)/g.exec('prefix-firstname prefix-lastname');
返回:
["prefix-firstname", "firstname"]
如何让它返回:
[
["prefix-firstname", "firstname"],
["prefix-lastname", "lastname"]
]
或者
["prefix-firstname", "firstname", "prefix-lastname", "lastname"]
最佳答案
这会做你想做的:
var str="prefix-firstname prefix-lastname";
var out =[];
str.replace(/prefix-(\w+)/g,function(match, Group) {
var row = [match, Group]
out.push(row);
});
可能是对 .replace 的误用,但我认为您不能将函数传递给 .match...
_佩兹
关于javascript - 使用正则表达式提取字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19190621/