DNS Hostname Regular Expression

A regular expression to match a valid hostname (also called domain label) in DNS entries. Useful if you have to parse through a lot of DNS entries and need something to help you make sense of it. You can also easily use this to check if a DNS entry is valid or not if you wanted to.

Note that a valid hostname may contain only the ASCII letters a through z (in a case-insensitive manner), the digits 0 through 9, and the hyphen-minus character (‘-‘).

/^(?![0-9]+$)(?!.*-$)(?!-)[a-zA-Z0-9-]{1,63}$/g

Matches:

  • regex
  • regex-pattern
  • regex01

Non-matches:

  • regex pattern
  • regex-
  • 100110

See Also: