Created
February 23, 2015 22:58
-
-
Save RavenHursT/fe8a95a59109096ac1f8 to your computer and use it in GitHub Desktop.
Javascript extract root domain from URL
This file contains hidden or 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
| var extractRootDomain = function(url){ | |
| return url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i)[1].split('.').slice(-2).join('.'); | |
| }; |
Author
@RavenHursT Except the test cases didn't output what it said it would, but actually printed:
"xyz.com"
"xyz.co.jp"
"co.uk"
"site.com"
"edu.au"
"example.website"
You can use tldts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote this years ago..
At this point, I would either look for a library to solve this problem or just ask AI..
Here's what Grok spit out: