Skip to main content
heresha93876846
Inspiring
November 7, 2016
Question

Convert javascript path to windows path

  • November 7, 2016
  • 3 replies
  • 1932 views

I have a string that contains a path that is formatted like this:

/C/Users/heresh/Desktop/output/2016

How can I convert this path to a windows path:

C:\Users\heresh\Desktop\output\2016

I know I should use:

path.replace() but I cant get it to work

This topic has been closed for replies.

3 replies

heresha93876846
Inspiring
November 7, 2016

I can't run this code:

var dirPathWin = name.replace("/", "\")

It doesnt work...

try67
Community Expert
Community Expert
November 7, 2016

A back-slash is a special character that needs to be escaped, so the correct code to use is:

name.replace("/", "\\");

However, this will only replace the first forward-slash in the string. To replace all of them you should use a Regular Expression, not just a string.

try67
Community Expert
Community Expert
November 7, 2016

It's a simple string manipulation.

- Replace all slashes with back-slashes.

- Remove the first back-slash.

- Replace the first back-slash with a colon followed by a back-slash.

You can read about the various String methods in JS here: JavaScript String Reference