DevKits

Web

Match a domain name

Flags: gi

Match domain names like example.com or sub.example.co.uk.

Pattern

Regular expression

(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}

How it works

Each label is 1–63 chars, starts and ends with alphanumeric, may contain hyphens. TLD is 2–63 alpha characters. This is a practical subset of RFC 1035 syntax that covers real-world domains.

Matches

  • example.com
  • sub.example.co.uk
  • a.io

Non-matches

  • -invalid.com (leading hyphen)
  • example (no TLD)

Common gotchas

This matches domain-only strings, not full URLs. Prepending `https?://` gives you the URL matcher. Also, this does not validate that the TLD actually exists — use the IANA TLD list for that.

Language snippets

JavaScript

const re = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}/gi;

Python

re.findall(r"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}", text, re.I)

Go

re := regexp.MustCompile(`(?i)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}`)

Java

Pattern.compile("(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z]{2,63}", Pattern.CASE_INSENSITIVE)

Frequently asked questions

What is the regex for domain name?

The pattern (?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63} (flags: gi) matches domain name. Each label is 1–63 chars, starts and ends with alphanumeric, may contain hyphens. TLD is 2–63 alpha characters. This is a practical subset of RFC 1035 syntax that covers real-world domains.

What does this domain name pattern match?

It matches strings like example.com, sub.example.co.uk, a.io, but rejects -invalid.com (leading hyphen), example (no TLD).

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

What are common pitfalls with a domain name regex?

This matches domain-only strings, not full URLs. Prepending `https?://` gives you the URL matcher. Also, this does not validate that the TLD actually exists — use the IANA TLD list for that.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester