i want to replace forward slash to backward slash i tried thisstring.replace.("/", "\\" )
but it shows like this C:\wdir/ReduxFlow.jpg
workout any idea?
Asked
Active
Viewed 50 times
1
-
already answer question looks like normal string but slash treats differently – Jan 29 '19 at 03:18
1 Answers
-1
split
and join
on /
and \
respectively.
var string="a/b/c/s";
console.log(string.split('/').join('\\'))

ellipsis
- 12,049
- 2
- 17
- 33
-
-
-
-
@Phil using regular expression has better performance and better readability – UtillYou Jan 29 '19 at 03:32
-
1@UtillYou can you link to a benchmark test? [This one](https://www.measurethat.net/Benchmarks/Show/1557/0/regex-vs-splitjoin) disagrees with you. Readability is subjective – Phil Jan 29 '19 at 03:33
-
2I didn't make a benchmark before. But I run test on this [benchmark](http://jsben.ch/LFfWA) several times. It seems these two methods have similar performance. But readability is very obvious. Since regular expression explicitly tells reader that we are replacing something. – UtillYou Jan 29 '19 at 03:47