如何在Javascript中的正则表达式中捕获特定的捕获组?

索拉夫·帕塔克
a = 'some random field="just_this" another="not_this"'

我想提取引号之间的文本,从field=. 在上述情况下,我想提取just_this. 我试过这样做。

a = 'some random field="just_this" another="not_this"'
pattern = /field="(\w+)"/g
console.log(a.match(pattern))

但这又回来了[ 'field="just_this"' ]如何提取just_this而不是field=部分。

急切

您可以使用积极的环顾来匹配应该紧跟在目标字符串之前/之后的内容而不将其包含在匹配中,但为什么不匹配整个字段名称?类似于(?<=some random field=")[^"]*?(?="\s) RegExr 的东西应该可以为您解决问题:

var a = a = 'some random field="just_this" another="not_this"';
pattern = /(?<=some random field=")[^"]*?(?="\s)/;
console.log(a.match(pattern));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章