How do I return the string example.com
using JavaScript if the input is https://example.com/page.html?query=string
?
Right now, if I use window.location.hostname
– or location.hostname
– the return value isexample.com
/
, but I wantexample.com
, without the trailing slash.
This question is similar to @r1853’s “How to remove trailing slash from window.location.pathname”, but because JavaScript provides many ways to grab parts of a URI (location.hostname
, location.host
, location.href
), and because wellformed URIs are a closed class, I had assumed there was a less expensive option than using a regular expression to remove trailing slashes from them.