Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 721
Radjiv Avatar asked Nov 28 '12 15:11

Radjiv


People also ask

How do you use forward slash in JavaScript?

As the forward slash (/) is special character in regular expressions, it has to be escaped with a backward slash (\). Also, to replace all the forward slashes on the string, the global modifier (g) is used. It will replace all the forward slashes in the given string.

What is the difference between forward slashes and backslashes?

There are two types of slashes: a backslash (\) and a forward slash (/). The backslash is used only for computer coding. The forward slash, often simply referred to as a slash, is a punctuation mark used in English.

How do you change backslash to forward slash?

Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.


1 Answers

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.

like image 149
David Pärsson Avatar answered Sep 20 '22 17:09

David Pärsson