开发者

how to replace javascript '\' with another char

i have a var in javascript that looks like this:

C:\docs\path\file.exe

how can i replace \ with another char like ? so the var becomes

C:?docs?path?file.exe

edit

i am trying to find the si开发者_如何学编程ze of a file in JS. the only way i managed to do it, is to call a [WebMethod] using $ajax. when i try to send the path as it is, i get an escape character error, so i chose to replace '\' by '?' and then the [WebMethod] replaces '?' with '\' check the file size and returns it.


You can do this:

yourVar = yourVar.replace(/\\/g, '?');

The backslash has to be doubled in the regular expression because it's special in that syntax. Otherwise, it's pretty simple.


escape the \ with \\ so

document.write(myPath.replace("\\", "?"));


var path = "C:\\docs\\path\\file.exe"
path ; //# => C:\docs\path\file.exe
path = path.replace(/\\/g, '?');
path ; //# => C:?docs?path?file.exe


var path = "C:\\docs\\path\\file.exe";
path = path.replace(/\\/g,"?");
alert(path);

You need to escape the "\" characters for this to work properly.


have u tried this ?

var str = "C:\docs\path\file.exe"
var newStr = str.replace(/\\/g,"?")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜