带有反斜杠的路径到带有正斜杠的路径 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13608564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:15:58  来源:igfitidea点击:

Path with backslashes to path with forward slashes javascript

javascriptreplacebackslash

提问by Radjiv

I'm trying to make a localxml file parsing "application" for some colleagues and i'm using the current function to retrieve the files:

我正在尝试为一些同事制作一个解析“应用程序”的本地xml 文件,我正在使用当前函数来检索文件:

function ShowFolderFileList(folderspec) {
    var fso, f, f1, fc, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.GetFolder(folderspec);
    fc = new Enumerator(f.files);
    s = "";
    for (; !fc.atEnd(); fc.moveNext()) {
        var pathString = fc.item();
        $("#test").append(pathString + "<br />");
    }
}

The problem with this function it returns a string similar to:

这个函数的问题是它返回一个类似于以下的字符串:

C:\Users\SomeUser\Desktop\cool\Archief\CDATA1.xml

I need to replace the backward slashes to forward slashes of the entire string. How to do this?

我需要将整个字符串的反斜杠替换为正斜杠。这个怎么做?

I tried the replace method:

我尝试了替换方法:

pathString.replace(/\/g, "/")

But it doesn't seem to do the trick.

但它似乎并没有做到这一点。

Can you guys help me out?

你们能帮我吗?

回答by David P?rsson

The replacemethod does not alter the current instance of the string, but returns a new one. See if this works:

replace方法不会改变字符串的当前实例,而是返回一个新实例。看看这是否有效:

pathString = pathString.replace(/\/g,"/");

See this example on jsfiddle.

在 jsfiddle 上查看此示例