How to Replace Double/Multiple Slash to Single in Url

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
  1. begin regex - /
  2. match one slash - /
  3. match another slash - /
  4. 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



Leave a reply



Submit