Here’s a JavaScript library that helps extracts part from the URL. i.e. TLD (e.g. .com), Second level domain e.g. google, and subdomain e.g. mail, and domain name: google.com.
The package does more than just string extractions, it also validates whether a domain is valid or not.