We're a place where coders share, stay up-to-date and grow their careers.
JavaScript:
const domainName = url => { let hostName = new URL(url).hostname; let domain = hostName; if (hostName != null) { let str = hostName.split('.').reverse(); if (str != null && str.length > 1) { domain = str[1] + '.' + str[0]; if (hostName.indexOf(/[^/]+((?=\/)|$)/g) != -2 && str.length > 2) { domain = str[2] + '.' + domain; } } } return domain.split('.')[0]; }
considering subdomains & second-level domains (ie. '.bc.ca')
JavaScript:
considering subdomains & second-level domains (ie. '.bc.ca')