title: What is the best method to swap words in a string
body: <p>Given the following string how would I swap the words right and left.</p>
  
  <p>i.e. change</p>
  
  <ul>
  <li>"left" -> "right"</li>
  <li>"right" -> "left"</li>
  </ul>
  
  <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>let str = JSON.stringify({
    val: 4,
    right: {
      val: 7,
      right: { val: 9, right: null, left: null },
      left: { val: 6, right: null, left: null },
    },
    left: {
      val: 2,
      right: { val: 3, right: null, left: null },
      left: { val: 1, right: null, left: null },
    },
  }, null, 2);
  
  str = str.replace((/"left"/g), o =&gt; { return "right1" }); 
  str = str.replace((/"right"/g), o =&gt; { return "left1" });
  
  console.log(); 
  console.log(str); 
  
  str = str.replace((/"right1"/g), o =&gt; { return "right" });
  
  console.log(); 
  console.log(str); 
  
  str = str.replace((/"left1"/g), o =&gt; { return "left" });</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm thinking of using str.replace(). </p>