How to Remove the Domain Name from a URL with JavaScript

Links in a chain, image made with DiffusionBee

I needed to remove the domain name from a URL for use in a link, as the only part of the URL I needed was the path. I knew, I had to use a regular expression, but regex isn't exactly my favourite discipline. As ever so often, I found the answer on StackOverflow.

For any arbitrary URL, assuming that the variable url contains your URL, you can do:

url = url.replace(/^.*\/\/[^\/]+/, '')

Comments