Skip to content

Instantly share code, notes, and snippets.

@VorticonCmdr
Created May 12, 2023 11:16
Show Gist options
  • Save VorticonCmdr/69c17d770cb0bf24a8f0e2d0d01bc08b to your computer and use it in GitHub Desktop.
Save VorticonCmdr/69c17d770cb0bf24a8f0e2d0d01bc08b to your computer and use it in GitHub Desktop.
chatGPT result on how to extract a domain from an url
function extractDomain(url) {
let domain;
// remove protocol
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
// remove port number
domain = domain.split(':')[0];
// remove subdomain, keeping multi-level domains like co.uk
const splitArr = domain.split('.');
const arrLen = splitArr.length;
// extracting the root domain here
if (arrLen > 2) {
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
// check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
// this is using a ccTLD
domain = splitArr[arrLen - 3] + '.' + domain;
}
}
return domain;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment