Skip to content

Instantly share code, notes, and snippets.

@gtchakama
Created June 7, 2023 14:02
Show Gist options
  • Save gtchakama/f2d232ca470fa4f7ce5f8e8525c332ec to your computer and use it in GitHub Desktop.
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.
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