Skip to content
Regex for Domain Name — Pattern Explained with Examples

Regex for Domain Name — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 2 min read

Domain name validation is essential for URL processing, email validation, and network configuration. This pattern checks that a string follows DNS label conventions: alphanumeric characters and hyphens, no leading or trailing hyphens, and a valid top-level domain.

The Pattern

/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/

Pattern Breakdown

PartMeaning
^Start-of-string anchor
(?:Non-capturing group for a single label plus dot
[a-zA-Z0-9]Label must start with a letter or digit
(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?Middle chars: 0–61 alphanumeric or hyphens, ending with alphanumeric
\.Literal dot separator
)+One or more label-dot groups (subdomains)
[a-zA-Z]{2,}TLD: at least two alphabetic characters
$End-of-string anchor

Matches

  • example.com
  • sub.domain.com
  • my-site.io
  • google.co.uk
  • api.v1.example.org

Does NOT Match

  • -example.com
  • example..com
  • .com
  • example.c (too short TLD)
  • exam ple.com
  • http://example.com

Language Examples

JavaScript

const domainRegex = /^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
console.log(domainRegex.test('example.com')); // true
console.log(domainRegex.test('-example.com')); // false

Python

import re
pattern = r'^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
print(bool(re.match(pattern, 'example.com')))  # True
print(bool(re.match(pattern, '-example.com')))  # False

Common Pitfalls

  • TLD minimum length is 2, but many new gTLDs are longer (e.g., .international, .technology) — the {2,} quantifier handles this correctly
  • Internationalized domain names (IDN) use punycode (xn--) prefixes which this ASCII-only pattern does not match
  • The full domain must be 253 characters or fewer, and each label maxes out at 63 characters — this pattern enforces label length but not total length
  • Do not include protocol (http://), port numbers, or path segments in domain validation — use a URL regex for those
  • Trailing dots (FQDN notation) are technically valid in DNS but rejected by this pattern

Real-World Use Cases

  • Email validation — the domain portion after @ must be a valid domain name
  • Link sanitization — extract and verify domain names from user-submitted URLs
  • DNS configuration tools — validate domain entries before making DNS API calls

FAQ

Does this pattern match internationalized domain names (IDN)?
No. IDNs use punycode encoding (e.g., xn--mgba3a4f16a.com) which contains characters outside the [a-zA-Z0-9-] range this pattern expects. Use a separate pattern that handles xn-- prefixed labels for full IDN support.
Should I allow trailing dots in domain validation?
Trailing dots (e.g., example.com.) represent the root zone in DNS and are technically valid. For user-facing validation, it is safer to reject trailing dots since users rarely intend to type them.

Related Patterns

Regex for Email Regex for URL

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro