15

I have a javascript variable comming from legacy system with backslashes into forward slashes:

'/46\465531_Thumbnail.jpg'

and I am trying to convert into this:

'/46/465531_Thumbnail.jpg'.

There is no way to fix the problem on the legacy system.

Here is the command I am running on IE8 browser:

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

as response I get:

---------------------------
Message from webpage
---------------------------
/46&5531_Thumbnail.jpg
---------------------------
OK   
---------------------------

actually I just want to be translated as '/46/465531_Thumbnail.jpg'

What is wrong?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161

4 Answers4

33

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Indeed I understand that. The problem is that I cannot manipulate the string, so I cannot modify manually the single backslash and put double backslashes. I am trying to do this `var test = escape("/46\465531_Thumbnail.jpg"); alert(teste.replace("%26",'/'));` – Junior Mayhé Jun 20 '11 at 21:32
  • 1
    How exactly is the string making it onto the page? If there is JavaScript code on the page that is dynamically created by the backend code, and it's *not* using "\\", then you're basically sunk; the generated code is simply broken. You won't be able to fix it reliably, because you won't be able to tell a "good" character from one that's the result of a messed-up backslash. – Pointy Jun 20 '11 at 21:34
  • 1
    Yes indeed, the solution was to fix the backend code that was messing the backslash. It is impossible to fix it using only javascript. – Junior Mayhé Jun 20 '11 at 21:44
  • Just a caveat that if you're using node's path.join, you need to chain the .replace AFTER the path.join so it looks like this: path.join(some,path,here).replace(/\\/g,"/"). – Robbie Smith Oct 29 '15 at 18:58
  • Not sure about the op, but I'm looking for a quick way to convert windows file paths to linux. So the string input isn't anything I can do anything about without compromising the whole point of a convenient methods to switch them – Shane May 20 '16 at 18:35
  • @Shane you can just `path = path.replace(/\\/g, "/");` but of course that won't deal with the leading `C:` etc. – Pointy May 20 '16 at 18:38
4

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/

mellamokb
  • 56,094
  • 12
  • 110
  • 136
1

The replacement part isn't the problem, it's the string itself. Your string:

"/46\465531_Thumbnail.jpg"

isn't /46\465531. Rather, the backslash is acting as an escape character. You need to change it to:

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

ie, escapeing the backslash with a backslash.

RHSeeger
  • 16,034
  • 7
  • 51
  • 41
0

Nothing wrong with the replace. The input is wrong.

javascript:alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
                     ^
                     \----------------  need to escape this!
Jon
  • 428,835
  • 81
  • 738
  • 806