JS 正则替换大括号、小括号、中括号的内容
替换大括号内的全部内容varstr="hello{name}";varnstr=str.replace(/\{[^\}]+\}/,"coffey");console.log(nstr);替换中括号内的全部内容varstr="hello[name]";varnstr=str.replace(/\[[^\)]*\]/g,"coffey");console.log(nstr);替换小括号内的全部内容va
·
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);
更多推荐
已为社区贡献3条内容
所有评论(0)