replaceAll is really new and, eg, not available on my version of node (14.16), but the normal replace 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):
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: