Created
December 20, 2023 16:56
-
-
Save dd701116/2f7437c6e8b17356851fcdddd6438a51 to your computer and use it in GitHub Desktop.
Get domain name
This file contains 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 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