Regex to Extract Domain Names
Extract domain names from URLs or plain text using this regex pattern. Captures root domains, subdomains, and compound TLDs. Free regex tool.
Regular Expression
/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,})+)/gi
Token Breakdown
| Token | Description |
|---|---|
| (?: | Start of non-capturing group |
| h | Matches the literal character 'h' |
| t | Matches the literal character 't' |
| t | Matches the literal character 't' |
| p | Matches the literal character 'p' |
| s | Matches the literal character 's' |
| ? | Makes the preceding element optional (zero or one times) |
| : | Matches the literal character ':' |
| \/ | Matches a literal forward slash |
| \/ | Matches a literal forward slash |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| (?: | Start of non-capturing group |
| w | Matches the literal character 'w' |
| w | Matches the literal character 'w' |
| w | Matches the literal character 'w' |
| \. | Matches a literal dot |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ( | Start of capturing group |
| [a-zA-Z0-9-] | Character class — matches any one of: a-zA-Z0-9- |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| {2,} | Matches 2 or more times |
| ) | End of group |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
Detailed Explanation
This regex extracts domain names from URLs or plain text. Here is the token-by-token breakdown:
(?:https?://)? — An optional non-capturing group that matches the protocol portion (http:// or https://). The entire group is optional thanks to the trailing ?, so the regex works on both full URLs and bare domain names.
(?:www.)? — Another optional non-capturing group matching the 'www.' prefix. Many domains are written with or without this prefix, so making it optional ensures both forms are captured.
( — Opens the first capturing group, which will contain the actual domain name we want to extract.
[a-zA-Z0-9-]+ — Matches the main part of the domain name. It allows letters, digits, and hyphens, requiring at least one character. This covers domain names like 'example', 'my-site', or 'shop123'.
(?:.[a-zA-Z]{2,})+ — A non-capturing group that matches a dot followed by two or more letters, repeated one or more times. This captures the TLD and allows for compound TLDs like .co.uk or .com.au.
) — Closes the capturing group.
The g flag enables global matching to find all domains in the text, and the i flag makes it case-insensitive. The first capturing group isolates the domain name without the protocol or www prefix, making extraction straightforward.
Example Test Strings
| Input | Expected |
|---|---|
| https://www.example.com | Match |
| blog.example.co.uk | Match |
| not a domain | No Match |
| http://shop.mystore.com/products | Match |
| just-plain-text | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
60 charsFlags: giMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match URLs
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/[\w./?#&=%-]*)*/gi
Regex for Email Validation
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i
Regex to Match IPv4 Addresses
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Regex to Match URLs with Query Parameters
/https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(?:\/[\w./-]*)*\?[\w=&%.+-]+(?:#[\w-]*)?/gi