1var str = "JavaScript replace method test";
2var res = str.replace("test", "success");
3//res = Javscript replace method success
1function replaceAll(str, find, replace) {
2 var escapedFind=find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
3 return str.replace(new RegExp(escapedFind, 'g'), replace);
4}
5//usage example
6var sentence="How many shots did Bill take last night? That Bill is so crazy!";
7var blameSusan=replaceAll(sentence,"Bill","Susan");
1let string = 'soandso, my name is soandso';
2
3let replaced = string.replace(/soandso/gi, 'Dylan');
4
5console.log(replaced); //Dylan, my name is Dylan
1var string = "Javascript is fun";
2var newString = string.replace("fun", "Awesome");
3console.log(newString); // Javascript is Awesome
1replace(regexp, newSubstr)
2replace(regexp, replacerFunction)
3
4replace(substr, newSubstr)
5replace(substr, replacerFunction)