在javascript中查找$ {和}之间的所有子字符串

时间:2016-03-15 06:50:13

标签: javascript regex

我有一些文本集合,其中包含$ {和}之间的一些文本,例如“this is $ {test} string $ {like}”。如何提取所有字符串。输出:测试,如

3 个答案:

答案 0 :(得分:1)

你可以尝试:

"this is ${test} string ${like}".match(/\${\w*}/g).map(function(str){return str.slice(2,-1)})
//["test", "like"]

答案 1 :(得分:1)

试试这个



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]);
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

match(/{[\w\d]+}/g);

例如

"{asdas}32323{234}".match(/{[\w\d]+}/g); //outputs ["{asdas}", "{234}"]

它将使用{}返回您可以从结果集中删除的匹配项

"{asdas}32323{234}".match(/{[\w\d]+}/g).map(function(value){return value.substring(1, value.length-1)}); //outputs ["asdas", "234"]