Created
May 12, 2023 11:16
-
-
Save VorticonCmdr/69c17d770cb0bf24a8f0e2d0d01bc08b to your computer and use it in GitHub Desktop.
chatGPT result on how to extract a domain from an url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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