在javascript中找到$ {和}之间的所有子字符串
问题描述:
我拥有文本集合,其中包含$ {和}之间的一些文本,比如“this is $ {test} string $ {like}”。我怎样才能提取所有字符串。输出:测试,像
答
尝试
match(/{[\w\d]+}/g);
例如
"{asdas}32323{234}".match(/{[\w\d]+}/g); //outputs ["{asdas}", "{234}"]
它将与{
和}
,使用它可以从ResultSet通过删除匹配返回
"{asdas}32323{234}".match(/{[\w\d]+}/g).map(function(value){return value.substring(1, value.length-1)}); //outputs ["asdas", "234"]
答
你可以尝试:
"this is ${test} string ${like}".match(/\${\w*}/g).map(function(str){return str.slice(2,-1)})
//["test", "like"]
答
尝试此
var str = "this is ${test} string ${like}";
var txt = str.match(/{[\w\d]+}/g);
for(var i=0; i < txt.length; i++) {
txt[i] = txt[i].replace(/[{}]/g, '');
alert(txt[i]);
}
'\ $ \ {([^}] *)\}' –
这不是McRegex驾车通过。 –