我试图解析以下类型的字符串:
[key:"val" key2:"val2"]
其中有任意键:“val”对在里面。我想获取键名和值。
对于那些好奇的人,我试图解析任务战士的数据库格式。
这是我的测试字符串:
[description:"aoeu" uuid:"123sth"]
这意味着除了空格之外,任何东西都可以放在键或值中,冒号周围没有空格,值总是在双引号中。
在node中,这是我的输出:
[deuteronomy][gatlin][~]$ node
> var re = /^\[(?:(.+?):"(.+?)"\s*)+\]$/g
> re.exec('[description:"aoeu" uuid:"123sth"]');
[ '[description:"aoeu" uuid:"123sth"]',
'uuid',
'123sth',
index: 0,
input: '[description:"aoeu" uuid:"123sth"]' ]
但是描述:“aoeu”也符合这个模式。我怎么能得到所有的比赛回来?
如果你有ES9
(意思是如果你的系统:Chrome, Node.js, Firefox等支持Ecmascript 2019或更高版本)
使用新的yourString。matchAll(/your-regex/g)。
如果你没有ES9
如果您使用的是较旧的系统,这里有一个简单的复制和粘贴函数
function findAll(regexPattern, sourceString) {
let output = []
let match
// auto-add global flag while keeping others as-is
let regexPatternWithGlobal = RegExp(regexPattern,[...new Set("g"+regexPattern.flags)].join(""))
while (match = regexPatternWithGlobal.exec(sourceString)) {
// get rid of the string copy
delete match.input
// store the match data
output.push(match)
}
return output
}
使用示例:
console.log( findAll(/blah/g,'blah1 blah2') )
输出:
[ [ 'blah', index: 0 ], [ 'blah', index: 6 ] ]
str.match(/regex/g)
以数组形式返回所有匹配项。
如果出于某种神秘的原因,您需要exec附带的额外信息,作为前面答案的替代方案,您可以使用递归函数来代替循环,如下所示(这看起来也很酷:)。
function findMatches(regex, str, matches = []) {
const res = regex.exec(str)
res && matches.push(res) && findMatches(regex, str, matches)
return matches
}
// Usage
const matches = findMatches(/regex/g, str)
正如前面的评论中所述,在regex定义的末尾加上g,以便在每次执行时将指针向前移动,这很重要。
这里有一个没有while循环的一行解决方案。
结果列表中保留该顺序。
潜在的缺点是
它为每个匹配复制正则表达式。
结果与预期的解形式不同。你需要再处理一次。
let re = /\s*([^[:]+):\"([^"]+)"/g
let str = '[description:"aoeu" uuid:"123sth"]'
(str.match(re) || []).map(e => RegExp(re.source, re.flags).exec(e))
[ [ 'description:"aoeu"',
'description',
'aoeu',
index: 0,
input: 'description:"aoeu"',
groups: undefined ],
[ ' uuid:"123sth"',
'uuid',
'123sth',
index: 0,
input: ' uuid:"123sth"',
groups: undefined ] ]