Radjiv
Radjiv

Reputation: 141

Path with backslashes to path with forward slashes javascript

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?

Upvotes: 14

Views: 22983

Answers (2)

PierBJX
PierBJX

Reputation: 2353

If in your string you do not have two backslashes and ONLY one backslash, you can use String.raw

var myString = String.raw`C:\Users\SomeUser\Desktop\cool\Archief\CDATA1.xml`;
console.log(myString.replace(/\\/g, '/'));

Upvotes: 4

David P&#228;rsson
David P&#228;rsson

Reputation: 6257

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.

Upvotes: 20

Related Questions