title: How to use insert with replace in JavaScript using Regex
body: <p>I'm trying to learn to use $$, $&amp;, $`,$',$n in my replace function. I am looking for some examples like this:</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>var re = /(\w+)\s(\w+)/;
  var str = "mango apple banana orange";
  var newstr = str.replace(re, "$2 $1");
  console.log(newstr);//outputs apple mango banana orange, (how does it do this?)</code></pre>
  </div>
  </div>
  
  Can anyone provide me with some examples and explanation on the use of each of these? I found out about them <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace" rel="nofollow noreferrer">here</a> and trying to understand.
  Thank you so much.</p>