1var myStr = 'this,is,a,test';
2var newStr = myStr.replace(/,/g, '-');
3
4console.log( newStr ); // "this-is-a-test"
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 a = 'a a a a aaaa aa a a';
2a.replace(/aa/g, 'bb');
3// => "a a a a bbbb bb a a"
1let re = /apples/gi;
2let str = "Apples are round, and apples are juicy.";
3let newstr = str.replace(re, "oranges");
4console.log(newstr)
5
6output:
7
8"oranges are round, and oranges are juicy."