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.

2
  • possible duplicate of Replacing all occurrences of a string in JavaScript Commented Sep 10, 2014 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 Commented Sep 10, 2014 at 17:05

1 Answer 1

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 : '/' });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.