1

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?

  • already answer question looks like normal string but slash treats differently –  Jan 29 '19 at 03:18

1 Answers1

-1

split and join on / and \ respectively.

var string="a/b/c/s";

console.log(string.split('/').join('\\'))
ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • upvote and mark it as correct so it can help others too – ellipsis Jan 29 '19 at 03:21
  • "a/b/c/s".replace(/\//g,'\\') would be better – UtillYou Jan 29 '19 at 03:25
  • @UtillYou how would you define _"better"_? – Phil Jan 29 '19 at 03:26
  • @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
  • 2
    I 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