1
// malformed string
var str = "C:\Windows\Fonts";

// C:WindowsFonts
alert(str.replace(/\\/g, "/"));

How do I correctly replace \ with / so I can get C:/Windows/Fonts?

user1643156
  • 4,407
  • 10
  • 36
  • 59
  • BTW you are aware that accessing `C:\` can be done only locally, not via the internet. – Joop Eggen Apr 08 '13 at 08:25
  • uhmmm....it's just a string example, and I'm not trying to access anywhere on the computer. Can we focus on Javascript? – user1643156 Apr 08 '13 at 08:28
  • Are you sure your string is properly comes from outer script? I did simple php , then got it in client page via ajax (jquery) $.ajax({ url: "temp.php", success: function(data) { alert(data); }}); and alert was proper c:\derp\herp as I expected. JS should handle such string normally. – Tommi Apr 08 '13 at 08:31

2 Answers2

1

Because it is special character use escape mark

var str = "C:\\Windows\\Fonts";
Robert
  • 19,800
  • 5
  • 55
  • 85
  • 1
    In javascript, `'` and `"` do the same thing. – slebetman Apr 08 '13 at 08:04
  • How come `str.replace(/\\/g, "/")` doesn't work? How do I correctly replace `\\` with `/`. – user1643156 Apr 08 '13 at 08:04
  • Replace only replaces 1st occurence. So you need to make sth like this. Or implement loop. str = str.replace("\\", "/").replace("\\", "/"); – Robert Apr 08 '13 at 08:12
  • 1
    @Robert ??? `only replaces 1st occurence` ??? I have the `g` flag, which means search and replace `g`lobally. – user1643156 Apr 08 '13 at 08:14
  • Of course if you add G then it'l replace all. str = str.replace("\\g", "/"); solves problem – Robert Apr 08 '13 at 08:16
  • @RobertPodwika `str = str.replace("\\g", "/");` your code does not work. you put the flag inside a string, and that will actually search for `\g`, not `\\`. – user1643156 Apr 08 '13 at 08:19
  • I run it with Chrome and it works for me. For cross browser compablity you should use RegExp and it's gonna work. – Robert Apr 08 '13 at 08:23
  • I found it on http://www.w3schools.com/jsref/jsref_replace.asp that it is possible to include g flag into string – Robert Apr 08 '13 at 08:28
  • @RobertPodwika I don't see a `g` inside quotes on that page. – user1643156 Apr 08 '13 at 08:30
  • this is not a global flag issue since OP says that not even first (single) slash is replaced – Tommi Apr 08 '13 at 08:33
  • Check Technical Details. More examples. – Robert Apr 08 '13 at 08:45
  • Unfortunately, I can't get it works as well as op. JS treats single slash with trailing character as escape sequence. var st = "a\b"; st.replace(/\\/, "/"); st.replace("\\", "/"); st.replace(/\\/g, "/"); all of this calls returns "a". But st.replace(/\b/g, "/") returns you /a/ (once at zero carret position as word boundary, second time as wrong recognized \b my malformed string). How to deal with it? – Tommi Apr 08 '13 at 09:20
0

You need to add one more backslash to show the special character.

var str = "C:\\Windows\\Fonts";
        alert(str);

Whenever you use the special character in JAvascript you need to add one backslash.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75