Pulling the host out of a URL is a one-liner if you use the built-in parser instead of a regex. Here’s how in JavaScript and Python, plus the details that catch people out — ports, embedded credentials, subdomains, and getting the root domain.
The URL object exposes the host directly. .hostname is the host without the port; .host includes it:
const u = new URL('https://user:pw@shop.example.com:8443/cart?x=1');
u.hostname; // "shop.example.com" (no port)
u.host; // "shop.example.com:8443" (with port)
u.port; // "8443"
u.origin; // "https://shop.example.com:8443"
Note the parser cleanly ignores the user:pw@ credentials and the path/query — you get just the host. That’s exactly why parsing beats a regex.
from urllib.parse import urlparse
u = urlparse('https://user:pw@shop.example.com:8443/cart?x=1')
u.hostname # 'shop.example.com' (lowercased, no port, no credentials)
u.port # 8443
u.netloc # 'user:pw@shop.example.com:8443' (full authority)
Here’s the catch: .hostname gives you the full host including subdomains (shop.example.com). Extracting the registrable root domain (example.com) is genuinely hard to do correctly, because you can’t just take the last two labels — example.co.uk and example.github.io have multi-part suffixes.
The correct approach uses the Public Suffix List (the maintained list of domain suffixes). Don’t hand-roll it — use a library:
// JavaScript: the 'tldts' or 'psl' package
import { parse } from 'tldts';
parse('https://shop.example.co.uk').domain; // "example.co.uk"
# Python: the 'tldextract' package
import tldextract
tldextract.extract('https://shop.example.co.uk').registered_domain
# 'example.co.uk'
A naive “last two labels” split would wrongly return co.uk here — which is why the Public Suffix List exists.
Hostnames can include ports, credentials, IPv6 literals ([::1]), and internationalized (Unicode) domains. A regex that handles all of these correctly is far harder to get right than new URL(str).hostname. Parse, don’t match.
Paste a URL into the URL parser to see the hostname, port, and origin isolated for you.
Written and maintained by the urlencodedecode.com team. Every technical claim on this page is verified against primary sources — the RFCs (3986, 3629, 4648, 7578), the WHATWG URL Standard, and official vendor or language documentation — rather than second-hand summaries. When a source contradicts a common assumption, we follow the source and note the discrepancy. Corrections: contactus@urlencodedecode.com.
Use the URL object: new URL(str).hostname returns the host without the port (e.g. "shop.example.com"). Use .host if you want the port included, and .origin for scheme + host + port. It ignores credentials and the path automatically.
urllib.parse.urlparse(str).hostname gives the full host, lowercased and without the port or credentials. To get the registrable root domain (example.co.uk from shop.example.co.uk), use the tldextract library, which consults the Public Suffix List.
hostname is just the host name (shop.example.com). host also includes the port when one is present (shop.example.com:8443). Use hostname when you only want the domain, host when the port matters.
Because some suffixes have multiple labels — example.co.uk or example.github.io. Taking the last two labels wrongly returns co.uk. The registrable domain must be computed against the Public Suffix List, which libraries like tldextract and tldts do for you.
No. Hostnames can include ports, embedded credentials, IPv6 literals, and internationalized characters, which a regex struggles to handle correctly. The built-in URL parser (new URL in JavaScript, urlparse in Python) is more reliable — parse, don't match.