With the following code:
var x = 'foo';
console.log(x.replace(x, "\\$&"));
The output is '\foo', as shown here: http://jsfiddle.net/mPKEx/
Why isn't it
'\\$&"?
I am replacing all of x with "\$&" which is just a plan old string so why is string.replace doing some crazy substitution when the 2nd arg of the function isn't supposed to do anything except get substitued in...