1const source = "abcdefabcdef";
2const str1 = "abc", str2 = "xyz";
3const reg1 = /abc/g, reg2 = "xyz";
4
5//Case 1 : When we want to replace a string by another
6console.log(source.split(str1).join(str2));
7console.log(source.replace(new RegExp(str1,"g"),str2));
8//versus
9console.log(source.replaceAll(str1,str2));
10
11//Case 2 : When we want to use a regular expression
12console.log(source.replace(reg1,reg2));
13//versus
14console.log(source.replaceAll(reg1,reg2));
15
16//Result = "xyzdefxyzdef"