title: How to escape ^ character in javascript?
body: <p>For a message in a specific message format (HL7) I'm trying to escape the <code>^</code> sign. So the string <code>abc^def^ghi</code> should become <code>abc\^def\^ghi</code></p>
  
  <p>I tried the following ways:</p>
  
  <pre><code>&gt; "abc^def^ghi".replace("^", "\^");
  'abc^def^ghi'
  &gt; "abc^def^ghi".replace("^", "\\^");
  'abc\\^def^ghi'
  &gt; "abc^def^ghi".replace("^", "\\\^");
  'abc\\^def^ghi'
  &gt; "abc^def^ghi".replace("\^", "\\\^");
  'abc\\^def^ghi'
  &gt; "abc^def^ghi".replace("\\^", "\\\^");
  'abc^def^ghi'
  &gt; "abc^def^ghi".replace(/^/g, "\\\^");
  '\\^abc^def^ghi'
  &gt; "abc^def^ghi".replace(/\^/g, "\\\^");
  'abc\\^def\\^ghi'
  &gt; "abc^def^ghi".replace(/\^/g, "\\^");
  'abc\\^def\\^ghi'
  &gt; "abc^def^ghi".replace(/\^/g, "\^");
  'abc^def^ghi'
  &gt; "abc^def^ghi".replace(/\\^/g, "\\\^");
  'abc^def^ghi'
  </code></pre>
  
  <p>As you can see, none of them work as I want them to. Does anybody know how I can do this?</p>