title: JavaScript String replace vs replaceAll
body: <p>ECMAScript 2021 has added a new String function <code>replaceAll</code>.
  A long time ago in a galaxy not so far away, people used <code>split</code> + <code>join</code> or regular expressions to <a href="https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript">replace all occurences of a string</a>.</p>
  <p>I created the following example to compare the new method to the old ones.
  While I could see some differences in the first case, for example I can't use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#specifying_a_string_as_a_parameter" rel="noreferrer">replacement patterns</a> with <code>split</code> + <code>join</code> or I need to escape special chars with <code>RegExp(str,&quot;g&quot;)</code>, I can't see any difference in the second case.</p>
  <p>What are the differences between the new method and the old ones (behaviour difference, performance, browser compatibility...) ?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const source = "abcdefabcdef";
  const str1 = "abc", str2 = "xyz";
  const reg1 = /abc/g, reg2 = "xyz";
  
  //Case 1 : When we want to replace a string by another
  console.log(source.split(str1).join(str2));
  console.log(source.replace(new RegExp(str1,"g"),str2));
  //versus
  console.log(source.replaceAll(str1,str2));
  
  //Case 2 : When we want to use a regular expression
  console.log(source.replace(reg1,reg2));
  //versus
  console.log(source.replaceAll(reg1,reg2));
  
  //Result = "xyzdefxyzdef"</code></pre>
  </div>
  </div>
  </p>