title: Escaping double quotes in JavaScript from a string in a variable
body: <p>I have these two scenarios:</p>
  <hr />
  <pre><code>console.log(('hello &quot;friend&quot; what\'s up?').replace(/\&quot;/g, '\\&quot;'));
  </code></pre>
  <p>I receive the expected result:</p>
  <blockquote>
  <p>hello &quot;friend&quot; what's up?</p>
  </blockquote>
  <hr />
  <p><strong>But</strong>, if I do this:</p>
  <pre><code>var val = 'hello &quot;friend&quot; what\'s up?';
  val.replace(/\&quot;/g, '\\&quot;');
  console.log(val);
  </code></pre>
  <p>I get...</p>
  <blockquote>
  <p>hello &quot;friend&quot; what's up?</p>
  </blockquote>
  <p><em>(the result needs to be <code>hello \&quot;friend\&quot; what's up?</code>)</em></p>
  <hr />
  <p>The only difference is that the second one uses an already created variable that contains the string. Why doesn't the second scenario actually replace the double quotes with <code>\&quot;</code>?</p>