user3386468
user3386468

Reputation: 106

How to replace character in URL string?

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.

Upvotes: 0

Views: 1098

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382454

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 : '/' });

Upvotes: 1

Related Questions