Skip to content

Instantly share code, notes, and snippets.

@dd701116
Created December 20, 2023 16:56
Show Gist options
  • Save dd701116/2f7437c6e8b17356851fcdddd6438a51 to your computer and use it in GitHub Desktop.
Save dd701116/2f7437c6e8b17356851fcdddd6438a51 to your computer and use it in GitHub Desktop.
Get domain name
function getDomainName(url){
function isDomainName(domain){
return domain.match(/^[a-zA-Z0-9][a-zA-Z0-9-\.]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/)!==null
}
let matches = url.match(/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/)
if(matches.length>=2){
let domain = matches[1]
return isDomainName(domain)? domain : null
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment