Benjamin
Benjamin

Reputation: 3826

changing file path model from Linux (/) to windows (\) in Java Script

I have some file paths in Unix system and I am trying to convert them to windows based path using java script Replace function.

For instance: I am trying to convert : //File/Test/images to \\File\Test\images

I am trying to achieve this by using string.replace which is

var winpath =oldPath.replace(/:|\\/g, "\/");

is this a correct way to replace the / to \?

Upvotes: 2

Views: 3024

Answers (3)

Girish
Girish

Reputation: 12117

Try this code, and escape \ char in replace() function second param

var oldPath = "//File/Test/images";
var winpath = oldPath.replace(/[\/]/g, "\\");
alert(winpath)

DEMO

Upvotes: 1

Amit Joki
Amit Joki

Reputation: 59232

You can do this:

var winPath = oldPath.replace(/\//g,"\\");

/\//g will match all the / and replaces it with \.

Upvotes: 1

Edi G.
Edi G.

Reputation: 2422

Use a regex literal with the g modifier, and escape the "/" with a "\" so it doesn't clash with the delimiters.

var myStr = '//File/Test/images', replacement = '';
var replaced = myStr.replace(/\//g, replacement);

Upvotes: 3

Related Questions