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 is : http://127.0.0.1:700/test/test/index.html

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

here is the demo: https://www.debuggex.com/r/dXZouvlec4srhg8i

where I am wrong?

Answers:

Answer

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.

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);

Answer
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.

Answer

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.

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.