0

Variable fileLocation may get two kinds of strings

  1. "N:\LDWG\PDF\2002P48.pdf"

  2. "\\Gis1\GIS\LDWG\Pdf\2007P69.pdf"

I just want result to be like

"N:/LDWG/PDF/2002P48.pdf"

"//Gis1/GIS/LDWG/Pdf/2007P69.pdf"

respectively

Tried

fileLocation.replace(/\\/g,"/")

str.replace("\\", "/")

But didn't work.

Note: There is a double slash in 2nd string

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
vipanth
  • 85
  • 1
  • 7
  • You must have double slashes in the strings, e.g.: `var s = "N:\\LDWG\\PDF\\2002P48.pdf"`. Then, the string will look like `N:\LDWG\PDF\2002P48.pdf`. Your `fileLocation = fileLocation.replace(/\\/g,"/")` will work. – Wiktor Stribiżew Feb 03 '16 at 13:01
  • `fileLocation.replace(/\\+/g,"/")` – Jai Feb 03 '16 at 13:02
  • Yeah, people were advising in other SO questions. But I don't how to do that, the data comes in that way. And also I wonder if it works for string, type 2 too – vipanth Feb 03 '16 at 13:03
  • You need to assign the value to the variable `fileLocation = fileLocation.replace(/\\/g,"/")` – jcubic Feb 03 '16 at 13:04
  • @Jai, It didn't work. Have a look at this [jsFiddle](https://jsfiddle.net/vipanth/wx5jLpsn/2/) – vipanth Feb 03 '16 at 13:11
  • @WiktorStribiżew, Also require solution for `\\Gis1\GIS\LDWG\Pdf\2007P69.pdf` – vipanth Feb 03 '16 at 13:13
  • You already have it. `"\\\\Gis1\\GIS\\LDWG\\Pdf\\2007P69.pdf".replace(/\\/g, "/")` (type this in the console, you will get `//Gis1/GIS/LDWG/Pdf/2007P69.pdf`). – Wiktor Stribiżew Feb 03 '16 at 13:15
  • @WiktorStribiżew Nice, looks like it works. But how to add those extra backslashes for each – vipanth Feb 03 '16 at 13:17
  • You have them if you read the strings from external source or get from a user, [your demo](https://jsfiddle.net/6vmm7ot3/) will work. – Wiktor Stribiżew Feb 03 '16 at 13:18
  • @WiktorStribiżew, Sorry, I didn't quite get that. All I found is that making `"\\Gis1\GIS\LDWG\Pdf\2007P69.pdf"` to `"\\\\Gis1\\GIS\\LDWG\\Pdf\\2007P69.pdf"` will solve my issue. How to get that. My variable will definitely be `"\\Gis1\GIS\LDWG\Pdf\2007P69.pdf"`like this initially. – vipanth Feb 03 '16 at 13:28
  • Then there is no solution. The system you get strings from should do that. – Wiktor Stribiżew Feb 03 '16 at 13:29
  • Ok, Thanks. I'll find a way out as suggested. – vipanth Feb 03 '16 at 13:32

0 Answers0