Domain Name
Match fully-qualified domain names like example.com or api.sub.example.co.uk.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,}", "g");
const input = "example.com";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}")
input_text = "example.com"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}`)
input := `example.com`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,} (flags: g)Raw source: (?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}
How it works
Examples
Input
example.comMatches
example.com
Input
api.sub.example.co.ukMatches
api.sub.example.co.uk
Input
not a domainNo match
—Common use cases
- •Extracting domains from email/URLs
- •DNS zone auditing
- •Allowlist / blocklist enforcement
- •Analytics referrer parsing
Related patterns
FTP URL
NetworkingMatch FTP and FTPS URLs, capturing optional credentials, host, port, and path.
Generic Connection String (URL Form)
NetworkingParse generic URL-form connection strings: `protocol://[user[:pass]@]host[:port][/database]`.
Hostname (Single Label, RFC 1123)
NetworkingValidate a single-label hostname per RFC 1123: 1–63 chars, letters/digits/hyphens, can't start or end with hyphen.
HTTP Status Code
NetworkingMatch 3-digit HTTP status codes in the 1xx–5xx range.