JS 正则替换大括号、小括号、中括号的内容
替换大括号内的全部内容var str=”hello {name}”;
var nstr = str.replace(/\{[^\}]+\}/,"coffey");
console.log(nstr);
替换中括号内的全部内容var str=”hello [name]”;
var nstr = str.replace(/\[[^\)]*\]/g,"coffey");
console.log(nstr);
替换小括号内的全部内容var str=”hello (name)”;
var nstr = str.replace(/\([^\)]*\)/g,"coffey");
console.log(nstr);
替换指定大括号内的全部内容var str=”hello {sex}{name}{sex}{name}”;
var nstr = str.replace(/\{name\}/g,"coffey");
console.log(nstr);
替换指定中括号内的全部内容var str=”hello [sex][name][sex][name]”;
var nstr = str.replace(/\[name\]/g,"coffey");
console.log(nstr);
替换指定小括号内的全部内容var str=”hello (sex)(name)(sex)(name)”;
var nstr = str.replace(/\(name\)/g,"coffey");
console.log(nstr);