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.
As a quick work aound I did the following:
badStr.replace(/\uffff/g, " ");
Which works as expected, and lets the user continue using the application until we find a better solution.
However, while I was playing around with this, I randomly tried a string replacement of "$$$$" which somehow got collapsed "$$".
You can see for yourself. Try pasting the following lines in your browser url bar:
javascript: alert(String.fromCharCode(0xffff).replace(/\uffff/g, "@@@@"));
results in @@@@
but
javascript: alert(String.fromCharCode(0xffff).replace(/\uffff/g, "$$$$"));
results in $$
This actually seems to be a problem with any string replacement, with $$$$ as the string replacement.
Both:
javascript: alert(String.fromCharCode(0x1234).replace(/\u1234/g, "$$$$"));
javascript: alert("hella".replace("h", "$$$$"));
result in the $$ collapse.
Any ideas as to why the string replacement behaves this way?