Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I remove duplicate forward slashes from the a url, but keep the // which comes after http: so that the URL does not break.

http://localhost//example/author/admin///

should be

http://localhost/example/author/admin/

I'm trying this, but it will remove only the last slash. I want to remove all double

abc = 'http://localhost//example/author/admin///';
clean_url = abc.replace(//$/,'');
alert(clean_url);

The following checks only three slashes.

clean_url = abc.replace("///", "");
alert(clean_url);

I want to remove all the duplicate slashes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
626 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use:

abc.replace(/([^:]/)/+/g, "$1");

Working Demo

Update: Already answered by Halcyon


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...