js如何用正则表达式替换字符中的首尾中括号[] 为{},而不影响中括号里面的中括号?


如把
"[jjj:111,[kkkk:222,ggg:333],mmm:444]"替换为
"{jjj:111,[kkkk:222,ggg:333],mmm:444}"

正则表达式 JavaScript


 var s = "[jjj:111,[kkkk:222,ggg:333],mmm:444]";
alert(s.replace(/^[\[]/,"{").replace(/[\]]$/,"}"));

把零食还我 answered 9 years ago

自己写了一个,
str = str.replace(/^\[(.*)\]$/, "\{$1\}")

IceMoon answered 9 years ago

Your Answer