18

I need a regular expression for replacing multiple forward slashes in a URL with a single forward slash, excluding the ones following the colon

e.g. http://link.com//whatever/// would become http://link.com/whatever/

4 Answers 4

40

I think this should work: /[^:](\/+)/ or /[^:](\/\/+)/ if you want only multiples.

It wont match leading // but it looks like you're not looking for that.

To replace:

"http://test//a/b//d".replace(/([^:]\/)\/+/g, "$1") // -->  http://test/a/b/d

Working Demo

Sign up to request clarification or add additional context in comments.

7 Comments

When i try to replace the matches with a single forward slash he first one returns http:/link.co/whateve/ and the second one ttp://link.co/whateve/ . For some reason it removes the first preceding character.
Oh, you want to replace them, and not just match them? That's an important difference.
@JeffShaver no : it remove some chars
@dystroy yeah, just saw that
In your regular expression you don't need the second match group: /([^:])\/\/+/g is enough
|
2

As you already accepted an answer. To show some more extend of matching and controlling the matches, this might help you in the future:

var url = 'http://link.com//whatever///';
var set = url.match(/([^:]\/{2,3})/g); // Match (NOT ":") followed by (2 OR 3 "/")

for (var str in set) {
    // Modify the data you have
    var replace_with = set[str].substr(0, 1) + '/';

    // Replace the match
    url = url.replace(set[str], replace_with);
}

console.log(url);

Will output:

http://link.com/whatever/

Doublets won't matter in your situation. If you have this string:

var url = 'http://link.com//om/om/om/om/om///';

Your set array will contain multiple m//. A bit redundant, as the loop will see that variable a few times. The nice thing is that String.replace() replaces nothing if it finds nothing, so no harm done.

What you could do is strip out the duplicates from set first, but that would almost require the same amount of resources as just letting the for-loop go over them.

Good luck!

Comments

0
result = subject.replace(/(?<!http:)\/*\//g, "/");

or (for http, https, ftp and ftps)

result = subject.replace(/(?<!(?:ht|f)tps?:)\/*\//g, "/");

2 Comments

The original question did not ask anything about replacing the scheme. This answer is not relevant.
@codeadventurer, this regular expression doesn't replace the scheme. This answer is relevant.
0

The original accepted answer does a sufficient job at replacing, but not for matching. And the currently accepted answer matches the character before duplicate slashes, also not good for matching.

Using a negative lookbehind to exclude the protocol from the match (?<!:), and a curly bracket quantifier to match 2 to infinite slashes \/{2,} does the job to both match and replace.

(?<!:)\/{2,}

let str = 'https://test.example.com:8080//this/is//an/exmaple///';
document.write('Original: ' + str + '<br><br>');
document.write('Matches: ' + str.match(/(?<!:)\/{2,}/g) + '<br><br>');
document.write('Replaced: ' + str.replace(/(?<!:)\/{2,}/g, '/'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.