Skip to content Skip to sidebar Skip to footer

How To Replace Double/multiple Slash To Single In Url

I have a url like this: http://127.0.0.1:7000//test//test//index.html expected output: http://127.0.0.1:7000/test/test/index.html I use this regex: [^http:](\/{2,}) and the output

Solution 1:

You may use

var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2"); // orvar res = s.replace(/(:\/\/)|(\/)+/g, "$1$2"); //  if you do not care of the : contextvar res = s.replace(/(?<!:)\/\/+/g, "/"); // Same as 2) if your environment supports ECMAScript 2018

See this regex demo or this regex demo, or yet another demo.

Details:

  • (https?:\/\/) - captures the http:// or https:// into Group 1
  • | - or
  • (\/)+ - one or more slashes are matched and only one / is kept in Group 2

In the replacement, $1 inserts the Group 1 contents back into the result (restoring the protocol) and the $2 backreference only inserts a single slash.

var s = "http://www.gogogogo.com//something//here";
var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");
console.log(res);

Solution 2:

var = 'http://127.0.0.1:7000//test//test//index.html';
str.replace(/([^:])(\/{2,})/g,"$1/");

The output is 'http://127.0.0.1:7000/test/test/index.html'.

The mode '[^http:]' means that not match h t p : , all these 4 characters.

Solution 3:

For an explanation of what is wrong with your regexp, you can try this online Regexp tester:

https://regex101.com/

For one thing, [^] is different from ^[]. [] checks for individual characters within except the special terms A-Z, a-z, 0-9, A-z etc...[^] matches characters that are not within.

So your regexp is basically this: Match an expression starting with a character than is not h, not t, not p, not:, and then followed by two or more /

The results are one 0// for the full match , and // for the () term, at the same location. The other //s are preceded by either : or t, and hence not matched.

Solution 4:

This method for PHP, but logic fo JS is same. Not use regexp for replace slashes in url. This method incorrect for many urls, like:

...com//test/////a///b//c//////

Regexp found all matches but you can't replace it correctly. Simple way use while or do, ex:

$req_uri = $_SERVER["REQUEST_URI"];
$s = "//";
$check = strstr($req_uri, $s);
while($check !== false){
    $req_uri = str_replace($s, "/", $req_uri);
    $check = strstr($req_uri, $s);
}

If you know better way - tell me.

Post a Comment for "How To Replace Double/multiple Slash To Single In Url"