ybabts
ybabts12mo ago

Parsing Hostname for TLD, domain name, and SLD

does anyone know of any packages to parse a URL's hostname for the top level domain, domain name, and sublevel domains? I found one, but it actually just doesn't work. It just errors saying the label is too short for everything I give it. https://github.com/lupomontero/psl
GitHub
GitHub - lupomontero/psl: JavaScript domain name parser based on th...
JavaScript domain name parser based on the Public Suffix List - GitHub - lupomontero/psl: JavaScript domain name parser based on the Public Suffix List
5 Replies
ybabts
ybabts12mo ago
I'm suprised this isn't already in the URL object for Javascript
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
ybabts
ybabts12mo ago
no I need the sub level domain of the url, which comes from the URL.hostname
// Sub level domain is "api"
// Domain Name is "geeksforgeeks"
// Top level domain is "org"
const url = new URL("https://api.geeksforgeeks.org:3000/pathname/?search=query");
// api.geeksforgeeks.org
console.log(url.hostname)
// Sub level domain is "api"
// Domain Name is "geeksforgeeks"
// Top level domain is "org"
const url = new URL("https://api.geeksforgeeks.org:3000/pathname/?search=query");
// api.geeksforgeeks.org
console.log(url.hostname)
NeTT
NeTT12mo ago
why not use some regex for that
const parseHostname = /(?:([a-z0-9]+)\.)?([a-z0-9]+)\.(?:([a-z]+)\.)?([a-z]+)/

const url = new URL("https://api.geeksforgeeks.org:3000/pathname/?search=query");

parseHostname.exec(url.hostname)
/*
[
"api.geeksforgeeks.org", // hostname
"api", // subdomain
"geeksforgeeks", // domain name
undefined, // sld
"org", // tld
index: 0,
input: "api.geeksforgeeks.org",
groups: undefined
]
*/
const parseHostname = /(?:([a-z0-9]+)\.)?([a-z0-9]+)\.(?:([a-z]+)\.)?([a-z]+)/

const url = new URL("https://api.geeksforgeeks.org:3000/pathname/?search=query");

parseHostname.exec(url.hostname)
/*
[
"api.geeksforgeeks.org", // hostname
"api", // subdomain
"geeksforgeeks", // domain name
undefined, // sld
"org", // tld
index: 0,
input: "api.geeksforgeeks.org",
groups: undefined
]
*/
I just woke up, there may be better ways to do it
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View