JavaScript在字符串中将所有/替换为\? [英] JavaScript replace all / with \ in a string?
问题描述
我有一个JavaScript文件,该文件通过使用cscript的Windows作业运行.但是,我似乎无法解决此问题以使其正常工作.在文件内部,它基本上采用URL并将其转换为UNC路径.
I have a javascript file that is ran through a windows job using cscript. 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.
例如: http://mysite.com/document1.htm 到\ myserver \ document1.htm
ex: http://mysite.com/document1.htm to \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?
谢谢,戴夫
推荐答案
是这样的:
str = str.replace(/\//g, "\\");
最后的/
是正常的/pattern/
格式,您需要为 \
转义添加一个额外的内容,
the /
on the end is the normal /pattern/
format, you need an extra for your \
escape, you can test it out here.
这篇关于JavaScript在字符串中将所有/替换为\?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!