Very simply, I want to assign a variable the value of a single backslash character. The problem is:
var myVar = '\'; // breaks because the backslash escapes the closing quote
var myVar = '\\'; // now myVar has two backslashes
Everything I can find says that you escape a backslash with a backslash, which is what I have always known to be generally true. However, when I run this in IE I get two backslashes instead of one.
Here's a screenshot of the IE debugger attempting to replace a # with a . This problem occurs anywhere I try to escape a backslash with a string literal - the string.replace() function yields the same error.
[Edit]
Thanks for the comments. In the short term I'll probably use octal or hexadecimal ascii as several people have recommended. But what I would really like is to understand why I can't just escape the backslash.
Here's a better screenshot without the string.replace function. Same result.
[/Edit]
Turns out @Prusse had it right: it wasn't a problem escaping the backslash, the problem was that the IE debugger renders '\'
as '\\'
. In my case I had an underlying problem that behaved the same way a mangled string would have so it took a while to track this down.
Solution:
Did you try to alert it? (with alert(myVar))
-Prusse