Converting backslashes into forward slashes using javascript does not work properly?

You need to double the backslash in your string constant:

alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));

If your legacy system is actually creating JavaScript string constants on your pages with embedded, un-quoted (that is, not doubled) backslashes like that, then it's broken and you'll have problems. However, if you're getting the strings via some sort of ajax call in XML or JSON or whatever, then your code looks OK.


It is actually interpreting \46 as an escape-code sequence for the character &. If you are going to hard-code the string, you need to escape the \:

alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
          ^^ change \ to \\

Sample: http://jsfiddle.net/6QWE9/