Reputation: 3826
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
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)
Upvotes: 1
Reputation: 59232
You can do this:
var winPath = oldPath.replace(/\//g,"\\");
/\//g
will match all the /
and replaces it with \
.
Upvotes: 1
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