For example, my directory is C:\Program Files\Git
, I want to change to /C/Program Files/Git
, how to use JavaScript to achieve string conversion?
Provided that the original path string is not changed.
Asked
Active
Viewed 69 times
0

Ross Ridge
- 38,414
- 7
- 81
- 112

shu
- 186
- 1
- 11
-
Possible duplicate of [How can I convert a windows path to posix path using node path](https://stackoverflow.com/questions/53799385/how-can-i-convert-a-windows-path-to-posix-path-using-node-path) and there is another libraries available on the internet. – Serge K. Feb 15 '19 at 15:05
-
1It's pretty unlikely that a simple string mapping like that would actually *work*. There's no "/C/Program Files" directory on any UNIX/Linux system I've ever seen. – Pointy Feb 15 '19 at 15:06
-
`"C:\\Program Files\\Git".replace(/\\/g, "/").replace(/(.):/,"/$1")` – Keith Feb 15 '19 at 15:08
-
Uh,you change the original string,You changed \ to \\ – shu Feb 15 '19 at 15:14
-
1You can't change an original string.. \\ that's escaping in JS, the real value is \ – Keith Feb 15 '19 at 15:15
-
1@Keith - those two `replace()` calls can be collapsed into a single regex replacement: `string.replace(/\\|^(.):/g, '/$1')` – dbenham Feb 15 '19 at 17:01
-
@dbenham The reason I did two was in case you had filenames with colons in, but colons don't appear to be valid filenames, so yes, that single regex would be better. – Keith Feb 15 '19 at 17:10