Regex for domain name

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

Options
Matches

Examples

Note

Each label is 1–63 characters and cannot start or end with a hyphen. Internationalised names need converting to punycode first.

Matches in the example

  • "example.com"
  • "sub.example.co.uk"
  • "my-site.dev"

Token by token

  1. \b a word boundary
  2. (?: a group, not capturing
  3. [a-z0-9] one of: “a” to “z” or “0” to “9”
  4. (?: a group, not capturing
  5. [a-z0-9-]{0,61} one of: “a” to “z”, “0” to “9” or “-”, between 0 and 61 times, as many as possible
  6. [a-z0-9] one of: “a” to “z” or “0” to “9”
  7. )? end of group, whole group optionally
  8. \. the character “.”
  9. )+ end of group, whole group one or more times, as many as possible
  10. [a-z]{2,63} one of: “a” to “z”, between 2 and 63 times, as many as possible
  11. \b a word boundary

Other patterns