Let’s say you want to fix a URL that looks like:
https://www.example.com/my/path//to-file.jpg
Using a string replace or a simple regex could incorrectly “fix” the double slashes following the protocol. We can fix that by using a negative lookbehind.
(?<!:)/+
For PHP:
<?php
$url = 'https://www.example.com/my/path//to-file.jpg';
$str = preg_replace('#(?<!:)/+#im', '/', $url);
// https://www.example.com/my/path/to-file.jpg
```
### For Javascript:
```
let url = 'https://www.example.com/my/path//to-file.jpg';
url.replaceAll(/(?<!:)\/+/gm, '/');
// "https://www.example.com/my/path/to-file.jpg"
```
Top comments (2)
replaceAll
is really new and, eg, not available on my version of node (14.16), but the normalreplace
works fine with the/g
flag.It is usually best to use libraries for things that have well specified structures. The libs will actually parse the string according to the spec and make sure everything is valid. I haven't done this in JS before, but looking at docs, it seems like it should be this (note that I don't have a Windows machine to test it on, I assume the path.posix should do it, but haven't verified):
Here, I've given it a pretty wonky looking path, but that path is valid. Yeah, you can apparently have colons in the path 🤷 (see
pchar
here).All that said, you actually can parse a URI with a regex, but it's a bit of a chore. Eg Ruby's standard library does it:
simple but effective, do you not need it to be
url = url.replaceAll
to work in JS though?