1

I tried to convert a url with backslashes into forward slashes but it showing me result like this : C:inetpubwwwrootmyprojectpdf,

 var path = "C:\inetpub\wwwroot\myproject\pdf";
 console.log(path);
 var path2 = path.replace(/\\/g, "/");
 console.log(path2);

seems backslashes url need to escape, I tried but failed.

  • 4
    Your pattern should work fine - the problem is that your *string* contains no literal backslashes. (a ``\`` before a character that can't be escaped is completely ignored) – CertainPerformance Dec 28 '18 at 07:55
  • in the string replace one `backslash` with two `backslash` the same way you had thought while doing the replace. – Kaushik Dec 28 '18 at 07:58
  • Hi kaushik, Problem is that am getting this url from database which is dynamic. I tried it but in dynamic state it doesn't work –  Dec 28 '18 at 08:22
  • The backslash ( \ ) is an escape character in Javascript. You can use double backslash( \\ ) like `var path = "C:\\inetpub\\wwwroot\\myproject\\pdf";` then after instead of replace you can use split() and join() e.g. `var path2 = path.split("\\"); var path_converted = path2.join("/"); console.log(path_converted);` This way is not recommended but you can use. – Jitendra G2 Dec 28 '18 at 08:27

0 Answers0