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.
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);
DEMO
I know there're different answers solving the same problem, just another approach :)
var s = "http://www.some-url.com//path//to";
var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");