How to replace backslashes with forward slashes in a malformed string with Javascript?

你离开我真会死。 提交于 2019-12-24 17:24:37

问题


// 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!