Domain Name Validation Regex
• CronOS Team
regexdomainvalidationtutorialdns
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Domain Name Validation Regex
Validate domain names with proper subdomain and TLD structure using regex pattern for lowercase alphanumeric domains.
Pattern Breakdown
regex
^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
([a-z0-9]+(-[a-z0-9]+)*\.)+ | Domain parts | One or more domain segments |
[a-z]{2,} | TLD | Two or more lowercase letters |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
([a-z0-9]+(-[a-z0-9]+)*\.)+- Domain parts (one or more):[a-z0-9]+- One or more lowercase letters or digits(-[a-z0-9]+)*- Zero or more hyphen-separated segments\.- Literal dot separator+- One or more domain parts (allows subdomains)
[a-z]{2,}- TLD: two or more lowercase letters
Examples
Valid:
example.comsubdomain.example.commulti-level.example.comtest-domain.co.ukexample123.orgmy-site.io
Invalid:
example(missing TLD)example.c(TLD too short)EXAMPLE.COM(uppercase not allowed)example..com(double dot)-example.com(starts with hyphen)example-.com(ends with hyphen)example.com.(trailing dot)
Implementation
JavaScript
javascript
const domainRegex = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/;
domainRegex.test('example.com'); // true
domainRegex.test('subdomain.example.com'); // true
domainRegex.test('test-domain.co.uk'); // true
domainRegex.test('example'); // false (missing TLD)
domainRegex.test('EXAMPLE.COM'); // false (uppercase)
Python
python
import re
domain_regex = r'^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$'
bool(re.match(domain_regex, 'example.com')) # True
bool(re.match(domain_regex, 'subdomain.example.com')) # True
bool(re.match(domain_regex, 'example')) # False (missing TLD)
Go
go
domainRegex := regexp.MustCompile(`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`)
domainRegex.MatchString("example.com") // true
domainRegex.MatchString("subdomain.example.com") // true
domainRegex.MatchString("example") // false (missing TLD)
Limitations
- Lowercase only: Only accepts lowercase letters
- No underscore: Doesn't allow underscores in domain names
- No multi-part TLD validation: Doesn't specifically validate
.co.ukformat - No length limits: Doesn't enforce DNS length limits (253 chars total, 63 per label)
- No IDN support: Doesn't support internationalized domain names
- Format only: Validates format, not actual DNS existence
When to Use
- Domain name format validation
- DNS-related applications
- Subdomain validation
- When you need strict format checking
- Domain input forms
For production, consider:
- Supporting uppercase (case-insensitive)
- Adding length validation (max 253 chars, 63 per label)
- Validating against known TLD lists
- Supporting internationalized domain names
- Combining with DNS lookup for existence verification
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!