d
Amit DhamuSoftware Engineer
 

Get domain without subdomain

1 minute read 00000 views

If you to retrieve the domain of a URL excluding the subdomain (eg. www), you can use this function

const getDomainWithoutSubdomain = url => {
  const urlParts = new URL(url).hostname.split('.')

  return urlParts
    .slice(0)
    .slice(-(urlParts.length === 4 ? 3 : 2))
    .join('.')
}