替换单个字符串:
1 var str="Visit Microsoft!" 2 console.log(str.replace(/Microsoft/,"W3School"));
替换多个字符串:
1 var str="Welcome to Microsoft! "; 2 str=str + "We are proud to announce that Microsoft has "; 3 str=str + "one of the largest Web Developers sites in the world."; 4 console.log(str.replace(/Microsoft/g, "W3School"))
替换转义字符:
1 var str="Welcome to "Microsoft"! "; 2 str=str + "We are proud to announce that "Microsoft" has "; 3 str=str + "one of the largest Web Developers sites in the world."; 4 console.log(str.replace(/Microsoft/g, "W3School")) 5 console.log(str.replace(new RegExp('"',"gm"),'"'))
参考地址: