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?
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.
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).
myString. replace("\\\\", "\\");
If you want to remove the first occurence: String[] parts = str. split("//", 2); str = parts[0] + "//" + parts[1]. replaceAll("//", "/");
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);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With