Answered
In my JavaScript code, I need to take a full URL and figure what the domain name is.
For example, this URL:
https://chunkks.com/questions/221/how-to-use-the-lodash-npm-package-in-node-js
I would need to extract the chunkks.com
domain name.
Thanks in advance for the help!
Try this:
const url = "https://cloud.digitalocean.com/login"
const {hostname} = new URL(url) // "cloud.digitalocean.com
You can use a regular expression to achieve this:
const url = "https://youtube.com/watch?v=K4ZAPAjMnOw"
var match = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i)
var domain = match && match[1] // "youtube.com"