0

字符串:

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

如何修改我编写的正则表达式以匹配 [2string](带整数的字符串),并将所有匹配替换为 [] 之间的引用值,因此它们变为 ['2string']

谢谢!

4

2 回答 2

2

Try this:

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

or use simple String.replace().

Working Fiddle

于 2013-06-24T06:08:06.130 回答
0

尝试

'some text [2string] some another [test] and [test-]'.replace(/\[(.*?)\]/g, "['$1']");
于 2013-06-24T06:08:19.193 回答