How to replace double/multiple slash to single in url
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.
How to replace double slash with single slash for an url
To avoid replacing the first // in http://
use the following regex :
String to = from.replaceAll("(?<!http:)//", "/");
PS: if you want to handle https use (?<!(http:|https:))//
instead.
Regular expression, replace multiple slashes with only one
Here you go:
$str = preg_replace('~/+~', '/', $str);
Or:
$str = preg_replace('~//+~', '/', $str);
Or even:
$str = preg_replace('~/{2,}~', '/', $str);
A simple str_replace()
will also do the trick (if there are no more than two consecutive slashes):
$str = str_replace('//', '/', $str);
Replace double slashes with single one, except for the first match in Ruby
You can use double look-behind assertions here:
(?<!http:)(?<!https:)//
Example:
s = 'https://mywebsite.com//hello/world//foo/bar'
s.gsub(Regexp.new('(?<!http:)(?<!https:)//'), '/')
# => "https://mywebsite.com/hello/world/foo/bar"
Regular Expression To Replace Double Slashes with Javascript
Replace all sequences of two or more /
with a single /
:temp.replace(/\/{2,}/g,'/');
PHP - reduce multiple slashes to single slash
str_replace may be faster in this case
$uri = str_replace("//","/",$uri)
Secondly: use trim: http://hu.php.net/manual/en/function.trim.php
$uri = trim($uri,"/");
How to replace multiple slashes (///) in a string to one slash (/) in Go lang?
Replace multiple slashes in text with single slash with Javascript
The answer is:
'one / two // three ///'.replace(/\/\/+/g, '/')
Let's go step by step to why it's correct.
First, to handle the error. It happened because the slash wasn't escaped. A regex begins with /, and to match all occurrences ends with /g, so to match all two slashes we would write:
/\/\//g
- begin regex - /
- match one slash - /
- match another slash - /
- all occurrences - /g
But then, given the input string above, the output would be:
one / two / three //
That is because ///
matches two pairs of slashes, for each pair turns it into one and that's it. Regexes aren't iterative. So what we're looking for is to match two slashes or more, which will give the answer I wrote at the beginning.
Note that this would also work:
/\/+/g
However it will have bad performance, since it will match single slashes and will replace them with the same string.
Regex: find all double slash after https?:// in link
Use a negative lookback:
s/(?<!http:|https:)\/\//\//g
(Or, possibly easier to see with an alternate regex delimiter of |
instead of /
):
s|(?<!http:|https:)\/\/|\/|g
Demo
Some versions of PCRE or Perl will give you Variable length lookbehind not implemented in regex m/(?<!http:|https:)///
. For those, you can make the lookback fixed width:
s/(?<!ttp:|tps:)\/\//\//g
Related Topics
How to Load All the Images from One of My Folder into My Web Page, Using Jquery/Javascript
Javascript: Location.Href to Open in New Window/Tab
Print Function Not Working in Chrome
How to Find Any Number That Includes a Certain Digit - Like 1323 Includes the Digit "2"
Display Images in React Using Jsx Without Import
How to Get Date Value from One Date Input Field and Put It to Other Date Field Using JavaScript
How to Apply Count Up Function to Multiple Values on the Page
Nosuchelementerror: Unable to Locate Element - Selenium Webdriver Using JavaScript
How to Prevent Form to Reload Page After Onsubmit
Check If Page Gets Reloaded or Refreshed in JavaScript
Regular Expression for Not Allowing Spaces in the Input Field
Angular 2 Concatenate Variable in Interpolation
Pass Dynamic Object in Onclick - Javascript/Jquery
Javascript Check If Values Exist in Array Json
Detect If a Field Is Updated With JavaScript or Jquery
How to Submit an HTML Form Without Redirection
Group By, and Sum, and Generate an Object for Each Array in JavaScript