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.
A double slash in the URL path is valid and will respond in the browser, but is typically unwelcome, as this could cause duplicate content issues if the CMS delivers the same content on two URLs (i.e. single slash and double slash).
The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.
You can use:
abc.replace(/([^:]\/)\/+/g, "$1");
Working Demo
Update: Already answered by Halcyon
This question has been answered before...
var str = 'http://localhost//example/author/admin///'; var clean_url = str.replace(/([^:])(\/\/+)/g, '$1/'); alert(clean_url);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With