2013-06-24 82 views
0

的字符串:正則表達式來修改與匹配替換模式

'some text [2string] some another [test] and [test-]'.match(/\[\D+?\]+/g); 

如何修改正則表達式我寫匹配也[2string(字符串整數),並與報價之間的值替換所有的比賽[ ],所以他們成爲['2string']

謝謝!

回答

2

試試這個:

var txt = 'some text [2string] some another [test] and [test-]'; 
var spl = txt.split('[').join("['").split(']').join("']"); 
console.log(spl); 

或使用簡單的與string.replace()。

Working Fiddle

0

嘗試

'some text [2string] some another [test] and [test-]'.replace(/\[(.*?)\]/g, "['$1']");