How to Parse URL in JavaScript into Host, Path, Search, and Hash
Dec 3, 2021
To parse a URL in JavaScript, use the new URL()
constructor.
This will create a new URL
object with hash
, host
, pathname
, search
, and hash
properties.
For the hash
and search
properties, it will default to an empty string if they do not exist on the URL.
const url = new URL('https://www.masteringjs.io/tutorials/fundamentals/parse-url.html?num=123')
url.href; // https://www.masteringjs.io/tutorials/fundamentals/parse-url.html?num=123
url.host; // www.masteringjs.io
url.pathname; // "/tutorials/fundamentals/parse-url.html"
url.search; // "?num=123"
url.hash; // ""
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!