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

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

javascript - remove duplicate forward slashes from the URL

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

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

1 Answer

0 votes
by (71.8m points)

You can use:

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

Working Demo

Update: Already answered by Halcyon


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...