1

In a case, I just wanted to replace all the back slashes with forward slash, but while trying to do that I am receiving some weird results.

Attempt 1:

"\\pest-TS01\Users\pest\Music\musi\New folder".replace(/\\/g, "/")

The above line yields the below result

"/pest-TS01UserspestMusicmusiNew folder"

Attempt 2:

var x = new RegExp("\\", "g");
"\\pest-TS01\Users\pest\Music\musi\New folder".replace(x, "/");

And the above code throws the following error,

Uncaught SyntaxError: Invalid regular expression: //: \ at end of pattern(…)


Expected result:

"//pest-TS01/Users/pest/Music/musi/New folder"

Can anyone give me a regex that matches the backslashes accurately? Also advise me on How to replace the matched back slashes with forward slashes. And I still believe that the regex that I have framed is correct, But why is it behaving weirdly?


Special note:

Please do not suggest any solutions using string manipulations like split() and something similar to that. I am looking for regex answers and need to find a reason why my regex is not working.

Mediocre
  • 281
  • 2
  • 9
  • 1
    You first expression is OK, your string is not. It must be `"\\\\pest-TS01\\Users\\pest\\Music\\musi\\New folder"` – Wiktor Stribiżew May 10 '16 at 09:34
  • Your pattern [`.replace(/\\/g, "/")`](https://regex101.com/r/bZ0bC8/2) works as expected. – Shafizadeh May 10 '16 at 09:36
  • 1
    @WiktorStribiżew Why would you say that? A correct pattern should work for every kind of string... So *your string isn't OK* makes no sense. – Shafizadeh May 10 '16 at 09:40
  • `"\testing\new\string"` contains a tab and a newline – mplungjan May 10 '16 at 09:41
  • @mplungjan But if I read that value from a textbox then it is coming as a plain string and my regex is working over that. why is it behaving differently with manually constructed string? – Mediocre May 10 '16 at 09:44
  • 3
    I don't understand downvotes. It is a valid question. – webduvet May 10 '16 at 09:46
  • You've already got the correct answer from @WiktorStribiżew (& mplungjan). Work with it. Here it is ones more - To have a backslash in a JS string you have to escape it. So `"\\"` isn't two backslases - it's one that's escaped. Regarding the down votes I agree - there unnecessary. – SamWhan May 10 '16 at 09:47
  • @ClasG Why would a backslash is not a double backslash visually when accessing it from a text box? – Mediocre May 10 '16 at 09:49
  • Just check [this fiddle](https://jsfiddle.net/ez0qe36s/) and see if you get it. – SamWhan May 10 '16 at 09:52
  • 1
    PS: I did not vote down - it is however a duplicate - for example http://stackoverflow.com/questions/2479309/javascript-and-backslashes-replace – mplungjan May 10 '16 at 09:53
  • This snippet shows what's the problem: https://jsfiddle.net/cp58pmgu – Tamas Rev May 10 '16 at 10:25
  • Use `String.raw()` on your string, check my answer. – Pedro Lobito May 10 '16 at 10:25

1 Answers1

2

Use String.raw(), convert single \ to \\ and finally \\ to /

string = String.raw`\\pest-TS01\Users\pest\Music\musi\New folder`;
result = string.replace(/\b[\\]{1}\b/g, "/").replace(/\\+/, "/");
document.write(result);

I honestly don't know what's happening behind the scenes with the singles back-slashes, but I guess they're being escaped.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268