Path with backslashes to path with forward slashes javascript

Radjiv picture Radjiv · Nov 28, 2012 · Viewed 18k times · Source

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

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?

Answer

David P&#228;rsson picture David Pärsson · Nov 28, 2012

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

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

See this example on jsfiddle.