字符串:
'some text [2string] some another [test] and [test-]'.match(/\[\D+?\]+/g);
如何修改我编写的正则表达式以匹配[2string](带整数的字符串),并将所有匹配项替换为[]之间的带引号的值,使它们成为['2string']
谢谢!
最佳答案
试试这个:
var txt = 'some text [2string] some another [test] and [test-]';
var spl = txt.split('[').join("['").split(']').join("']");
console.log(spl);
或者使用简单的 String.replace()。
关于javascript - 正则表达式修改与替换模式的匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17268920/