简体   繁体   English

regexp修改匹配替换模式

[英]regexp to modify matches with replace pattern

The string: 字符串:

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

How to modify regexp i've written to match also [2string] (string with integer), and replace all the matches with quoted values between [], so they become ['2string'] 如何修改regexp我写的也匹配[2string](带整数的字符串),并用[]之间的引用值替换所有匹配,所以它们变为['2string']

thanks! 谢谢!

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(). 或使用简单的String.replace()。

Working Fiddle 工作小提琴

尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM