[英]JavaScript replace all / with \ in a string?
I have a javascript file that is ran through a windows job using cscript. 我有一个使用cscript通过Windows作业运行的javascript文件。 However, I can't seem to fix this thing to work correctly.
但是,我似乎无法解决这个问题。 Inside the file, it basically takes a URL and transforms it to a UNC path.
在文件内部,它基本上采用URL并将其转换为UNC路径。
ex: http://mysite.com/document1.htm to \\myserver\\document1.htm 例如: http : //mysite.com/document1.htm到\\ myserver \\ document1.htm
However, I can't seem to get the /'s to goto \\'s and am at a loss why. 但是,我似乎无法获得改编,而且不知道为什么。
I've tried 2 things basically 我基本上尝试了两件事
1) str = str.replace(/\/g, "\\");
2) str = str.replace("/", "\\");
Any idea why it wont work? 知道为什么它不会工作吗?
Thanks, Dave 谢谢,戴夫
It's like this: 就像这样:
str = str.replace(/\//g, "\\");
the /
on the end is the normal /pattern/
format, you need an extra for your \\
escape, you can test it out here . /
最后是普通/pattern/
格式,你需要一个额外的\\
escape, 你可以在这里测试它 。
You can use the following trick: 您可以使用以下技巧:
str = str.split("/").join("\\");
More generally: 更普遍:
function replaceAll(str, a, b) {
return str.split(a).join(b);
}
This avoids regular expression nightmares. 这避免了正则表达的噩梦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.