JS 正则替换大括号、小括号、中括号的内容

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);

    原文作者:会飞的哈士奇
    原文地址: https://blog.csdn.net/dongyan3595/article/details/124866045
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞