Regex for Domain Name
Matches valid domain names with proper label formatting. Each label can be up to 63 characters.
Pattern
/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/Pattern Breakdown
| Token | Explanation |
|---|---|
| ^ | Start of string |
| [a-zA-Z0-9] | Label starts with alphanumeric |
| (?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? | Optional middle characters (up to 61) ending with alphanumeric |
| \.)+ | Dot separator, repeat for each subdomain |
| [a-zA-Z]{2,} | TLD (2+ letters) |
| $ | End of string |
Test Examples
| Input | Match? |
|---|---|
| example.com | Match |
| sub.domain.co.uk | Match |
| my-site.org | Match |
| -invalid.com | No match |
| no_underscores.com | No match |
Common Use Cases
- DNS configuration validation
- URL parsing
- Certificate management
Variations
^[\w.-]+\.[a-zA-Z]{2,}$Simpler domain match
Other Regex Patterns
Want to test this pattern with your own data?
Open the Regex Tester