提取“wrapped"在javascript中使用正则表达式从字符串中获取值

extract "wrapped" values from a string using regex in javascript

本文关键字:正则表达式 字符串 获取 javascript wrapped quot 提取      更新时间:2023-09-26

我有一个字符串如下:

randomword,random${notavalue;randomword${iwantthisrandompart1}randomword${iwantthisrandompart2}randomword

如何只提取${}内的值,即本例中的iwantthisrandompart1iwantthisrandompart2

var s="randomword,random${notavalue;randomword${iwantthisrandompart1}randomword${iwantthisrandompart2}randomword"
var myRe=/'$'{([^}{]*)'}/g
var m=myRe.exec(s)
while (m!=null) {alert(m[1]);m=myRe.exec(s)}

试一试。您需要的部分将被"提醒"

这会解决你的麻烦

(?<='$'{)[^'$'{]*(?='})

regex演示在这里:http://regex101.com/r/oY0bU9

更新:由于javascript不支持向后看

(?='$'{)'$'{([^'$'{]*(?='}))

demo here: http://regex101.com/r/rJ3nI3