字符串:
'some text [2string] some another [test] and [test-]'.match(/\[\D+?\]+/g);
如何修改我编写的正则表达式以匹配 [2string](带整数的字符串),并将所有匹配替换为 [] 之间的引用值,因此它们变为 ['2string']
谢谢!
字符串:
'some text [2string] some another [test] and [test-]'.match(/\[\D+?\]+/g);
如何修改我编写的正则表达式以匹配 [2string](带整数的字符串),并将所有匹配替换为 [] 之间的引用值,因此它们变为 ['2string']
谢谢!
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().
尝试
'some text [2string] some another [test] and [test-]'.replace(/\[(.*?)\]/g, "['$1']");