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

TokenDescription
(?:Start of non-capturing group
hMatches the literal character 'h'
tMatches the literal character 't'
tMatches the literal character 't'
pMatches the literal character 'p'
sMatches 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
wMatches the literal character 'w'
wMatches the literal character 'w'
wMatches 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

InputExpected
https://www.example.comMatch
blog.example.co.ukMatch
not a domainNo Match
http://shop.mystore.com/productsMatch
just-plain-textNo Match

Try It — Interactive Tester

//gi
gimsuy

Match Highlighting(3 matches)

https://www.example.com blog.example.co.uk not a domain http://shop.mystore.com/products just-plain-text

Matches & Capture Groups

#1https://www.example.comindex 0
Group 1:example.com
#2blog.example.co.ukindex 24
Group 1:blog.example.co.uk
#3http://shop.mystore.comindex 56
Group 1:shop.mystore.com
Pattern: 60 charsFlags: giMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →