/^/
EasyNetworking4 min read

Hostname Regex

Validates an RFC 1123 style hostname made of one or more dot-separated labels, where every label starts and ends with an alphanumeric character and may contain interior hyphens.

#hostname#networking#dns#validation#server

Regex Pattern

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

Pattern Breakdown

Hover over a token to see what it does.

^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
TokenMeaning
^Anchors the match to the start of the string
[a-zA-Z0-9]The first label must start with a letter or digit
(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?Optional middle and final characters of the first label, allowing interior hyphens
(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*Zero or more additional dot-separated labels, each following the same start/end alphanumeric rule
\.Literal dot separating labels when a hostname has more than one part
$Anchors the match to the end of the string

Detailed Explanation

What it does

This pattern checks that a string is a valid hostname: a single label like localhost, or multiple dot-separated labels like db.internal.example, where no label starts or ends with a hyphen and no label is empty.

Why it works

Unlike a fully qualified domain name, a hostname does not require a dot or a specific top-level domain, so the trailing label group is wrapped in `*` rather than requiring at least one repetition. Each label enforces the RFC 1123 rule that hyphens are only valid in the interior by pinning the first and last characters to alphanumerics, which naturally rejects leading or trailing hyphens and consecutive dots (which would create an empty label).

Common use cases

  • Validating a server or container hostname field in infrastructure configuration tools
  • Checking user-supplied hostnames before passing them to a DNS resolver or SSH client
  • Sanitizing hostnames in configuration files for reverse proxies or load balancers
  • Client-side validation for a 'connect to host' input in an admin dashboard

Edge cases

  • Single-label names like localhost or my-laptop are valid hostnames under this pattern
  • IPv4 or IPv6 addresses are not matched by this pattern since it targets alphanumeric labels only
  • Underscores are technically used in some non-standard hostnames (e.g. certain SRV-style names) but are rejected here per strict RFC 1123 rules
  • The overall 253-character total hostname length limit is not separately enforced beyond the 63-character per-label bound

Limitations

  • Does not verify that the hostname resolves via DNS or is reachable on the network
  • Does not support internationalized hostnames without Punycode pre-conversion
  • Does not distinguish a bare hostname from a fully qualified domain name; both shapes are accepted
  • Underscore-containing hostnames used by some legacy or internal systems will be rejected

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

localhost server01 my-host

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const hostnameRegex = /^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
console.log(hostnameRegex.test('localhost')); // true

Common Mistakes

Requiring at least one dot, which incorrectly rejects valid single-label hostnames like localhost

Fix: Make the trailing label group repeat with `*` instead of `+` so a bare hostname without a dot is still valid

Allowing underscores because they show up in some internal hostnames

Fix: Stick to the RFC 1123 alphanumeric-and-hyphen character set unless you specifically need to support legacy underscore hostnames

Not anchoring each label's start and end characters separately, allowing leading or trailing hyphens

Fix: Pin the first and last character of every label to `[a-zA-Z0-9]` and restrict hyphens to the interior

Performance Notes

  • Each label is capped at 63 characters, which bounds backtracking within any single label
  • The `*` on the trailing label group is safe from catastrophic backtracking because each iteration must consume a literal dot, giving the engine a clear checkpoint
  • Reuse a single compiled regex instance when validating hostnames in a hot path like a request handler

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes