How to replace double/multiple slash to single in url

I have a url like this:

expected output:

I use this regex: [^http:](\/{2,})

and the output is :

the matches are: '0//' '//'

here is the demo:

where I am wrong?



You may use

var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2"); // or
var res = s.replace(/(:\/\/)|(\/)+/g, "$1$2"); //  if you do not care of the : context
var 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.


  • (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 = "";
var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");

var = '';

The output is ''.

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


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

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.


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:

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.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.