How do you safely wrap a JS string variable in double quote chars?
<p>Obviously when you're creating an actual string literal yourself,   you backslash escape the double quote characters yourself.  </p>

<pre><code>var foo = "baz\"bat";
</code></pre>

<p>Just as you would with the handful of other control characters, like linebreaks and backslashes. </p>

<pre><code>var bar = "baz\\bat\nmynew line and a \"quote\" ";
</code></pre>

<p>but if you're just wrapping that existing variable in quote character, ie to give it to some other system that requires quoted input, there's some confusion.  </p>

<p>Obviously you have to escape any potential double quote characters that are in the string. </p>

<pre><code>var doubleQuoteRe = /\"/g;
var quoted = "\"" + unquoted.replace(escaper, '\\\"') + "\"";
</code></pre>

<p>But according to some you <em>also</em> now have to worry about escaping literal backslash characters in the variable.   In other words using much bigger hammer than my little regex.   However i dont see why.  </p>
