Skip to main content
CalcHive

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

TokenExplanation
^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

InputMatch?
example.comMatch
sub.domain.co.ukMatch
my-site.orgMatch
-invalid.comNo match
no_underscores.comNo match

Common Use Cases

  • DNS configuration validation
  • URL parsing
  • Certificate management

Variations

^[\w.-]+\.[a-zA-Z]{2,}$

Simpler domain match

Want to test this pattern with your own data?

Open the Regex Tester