0

How can I convert the following URL string

[IpAddress]/Folder/\\2014\\5\\5\\abc\\\\cde\\efg\\\\IR12345676765454554\\123456.jpg]

to

[IpAddress]/Folder/2014/5/5/abc/cde/efg/IR12345676765454554/123456.jpg]

Thanks in advance.

Huangism
  • 16,278
  • 7
  • 48
  • 74
user3386468
  • 96
  • 1
  • 2
  • 13
  • possible duplicate of [Replacing all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – Dave Sep 10 '14 at 16:24
  • Don't think all the replacements are equal here. At one place /\\ is replaced with only /, then \\ is replaced with / and again \\\\ is replaced with /. Its not clear what the actual objective is – nitigyan Sep 10 '14 at 17:05

1 Answers1

1

It looks like you want to replace every sequence of / and \ into a single /. Here's a way to do it :

str = str.replace(/[\/\\]+/g, '/');

EDIT

for your new question in which you don't want to replace the double / of "http://" (and I guess "file://", etc), you can do this :

str = str.replace(/(:?)([\/\\]+)/g, function(_,d,s){ return d ? d+s : '/' });
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758