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 randomtext = "Random text yo!"
2let textRegex = /yo!/;
3randomtext.replace(textRegex, "is here.");
4console.log(randomtext.replace(textRegex, "is here."));
5// Returns Random text is here.
1// Replace with no modifiers
2let newText = startText.replace("yes", "no")
3console.log(newText) // "Yes, I said no, it is, yes."
4
5// Replace with 'g'-global modifier
6newText = startText.replace(/yes/g, "no")
7console.log(newText) // "Yes, I said no, it is, no."
8
9// Replace with modifiers 'g'-global and 'i'-case insensitive
10newText = startText.replace(/yes/gi, "no")
11console.log(newText) // "no, I said no, it is, no."