SO re有很多问题:方括号,括号等中的匹配,但是我想知道如何在一个字符(parens)中进行匹配,而忽略该字符
"Key (email)=([email protected]) already exists".match(/\(([^)]+)\)/g)
// => ["(email)", "([email protected])"]
我相信这是因为JS不支持[^)]
。鉴于此,您将如何建议在parens中提取值。
我宁愿选择.replace('(', '').replace(')', '')
比值不那么古怪的东西
我希望返回值是 ["email", "[email protected]"]
使用以下两个非常简单的示例:
.match()
提取和构建阵列var arr = "Key (email)=([email protected]) already exists".match(/(?<=\()[^)]+(?=\))/g);
console.log( arr );
https://regex101.com/r/gjDhSC/1
MDN-String.prototype.match()
.replace()
提取的价值观和推到数组var arr=[];
"Key (email)=([email protected]) already exists"
.replace(/\(([^)]+)\)/g, (p1, p2) => arr.push(p2) );
console.log( arr );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句