Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 336
Jack Avatar asked Nov 17 '16 07:11

Jack


People also ask

How do you fix a double slash in URL?

If the double slash in the page's permalink is generated by your CMS, you might need to address your developer for help. If the URL with a double slash is indexed in Google or has incoming external links, you can set the proper 301 redirects to the corrected URL.

Can URL have two 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).

How do you change a double slash with a single slash in Java?

myString. replace("\\\\", "\\");

How do you get rid of a double slash in Java?

If you want to remove the first occurence: String[] parts = str. split("//", 2); str = parts[0] + "//" + parts[1]. replaceAll("//", "/");


3 Answers

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);
like image 188
Wiktor Stribiżew Avatar answered Oct 19 '22 14:10

Wiktor Stribiżew


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.

like image 30
allen Avatar answered Oct 19 '22 13:10

allen


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.

like image 1
alex Avatar answered Oct 19 '22 13:10

alex