2016-03-15 64 views
-4

我擁有文本集合,其中包含$ {和}之間的一些文本,比如「this is $ {test} string $ {like}」。我怎樣才能提取所有字符串。輸出:測試,像

+0

'\ $ \ {([^}] *)\}' –

+3

這不是McRegex駕車通過。 –

回答

0

嘗試

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"] 
1

你可以嘗試:

"this is ${test} string ${like}".match(/\${\w*}/g).map(function(str){return str.slice(2,-1)}) 
//["test", "like"] 
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]); 
 
}

相關問題