title: Javascript string replace weirdness -- $$$$ gets collapsed to $$ -- what's the reason behind this result?
body: <p>At work, I was encountering a problem where users of our application were receiving messages featuring an invalid unicode character (0xffff), which according to the standard, should never be mapped to a symbol.</p>
  
  <p>As a quick work aound I did the following:</p>
  
  <pre><code>badStr.replace(/\uffff/g, " ");
  </code></pre>
  
  <p>Which works as expected, and lets the user continue using the application until we find a better solution.</p>
  
  <p>However, while I was playing around with this, I randomly tried a string replacement of "$$$$" which somehow got collapsed "$$".</p>
  
  <p>You can see for yourself. Try pasting the following lines in your browser url bar:</p>
  
  <pre><code>javascript: alert(String.fromCharCode(0xffff).replace(/\uffff/g, "@@@@"));
  </code></pre>
  
  <p>results in @@@@</p>
  
  <p>but</p>
  
  <pre><code>javascript: alert(String.fromCharCode(0xffff).replace(/\uffff/g, "$$$$"));
  </code></pre>
  
  <p>results in $$</p>
  
  <p>This actually seems to be a problem with any string replacement, with $$$$ as the string replacement.</p>
  
  <p>Both:</p>
  
  <pre><code>javascript: alert(String.fromCharCode(0x1234).replace(/\u1234/g, "$$$$"));
  javascript: alert("hella".replace("h", "$$$$")); 
  </code></pre>
  
  <p>result in the $$ collapse.</p>
  
  <p>Any ideas as to why the string replacement behaves this way?</p>