问题
// 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
?
回答1:
Because it is special character use escape mark
var str = "C:\\Windows\\Fonts";
回答2:
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.
来源:https://stackoverflow.com/questions/15874048/how-to-replace-backslashes-with-forward-slashes-in-a-malformed-string-with-javas