Amit Dhamu

Software Engineer

Get domain without subdomain

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('.')
}

 url

Find a HTML Element With Text

Show Comments

Made with by Amit Dhamu.
© MMXXI. All rights reserved.