Created
June 7, 2023 14:02
-
-
Save gtchakama/f2d232ca470fa4f7ce5f8e8525c332ec to your computer and use it in GitHub Desktop.
A function that when given a URL as a string, parses out just the domain name and returns it as a string.
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 domainName(url) { | |
// Remove protocol and www. prefix from domain | |
let domain = url.replace(/(https?:\/\/)?(www\.)?/, ''); | |
// Remove everything after the first dot (including the dot) | |
domain = domain.split('.')[0]; | |
return domain; | |
} | |
console.log(domainName("http://github.com/carbonfive/raygun")); // "github" | |
console.log(domainName("http://www.zombie-bites.com")); // "zombie-bites" | |
console.log(domainName("https://www.cnet.com")); // "cnet" | |
console.log(domainName("http://www.google.com")); // "google" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment