Reputation:
How could I merge both those regular expressions? This is interesting because it would allow to match the beginning of a string and the end without touching the content in the middle.
function cleanURL(url) {
url = url.replace(/^(?:https?:\/\/)?(?:www\.|ww\d\.|w\d\d\.)?/, '')
url = url.replace(/(\/.*)/, '')
return url
}
console.log(cleanURL('https://hello-world.example.tld/yello-blue/green'))
Result: hello-world.example.tld
Upvotes: 0
Views: 38
Reputation: 160170
Rather than trying to use regex to tease out parts of URLs just use the URL
class:
new URL("https://hello-world.example.tld/yello-blue/green").hostname
That doesn't mean it can't be done with a regex, you just need to look for whatever is between //
and /
.
All this said, I'm not sure what your actual intent is, because there's a bunch of shenanigans with www
etc. The URL
approach won't filter, it'll just return the hostname.
Upvotes: 1