Networking

Hostname (Single Label, RFC 1123)

Validate a single-label hostname per RFC 1123: 1–63 chars, letters/digits/hyphens, can't start or end with hyphen.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^[a-zA-Z0-9](?:[a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?$", "");
const input = "web-01";
const match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

Pattern

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

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

How it works

^[a-zA-Z0-9] requires the first character to be a letter or digit. (?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])? optionally matches up to 61 more characters (letters, digits, hyphens) followed by a letter/digit ending — preventing trailing hyphens. Combined max length: 63 characters per RFC 1123.

Examples

Input

web-01

Matches

  • web-01

Input

host123

Matches

  • host123

Input

-bad-start

No match

—

Common use cases

  • •Kubernetes pod and service name validation
  • •DNS label sanity checking
  • •Container hostname enforcement
  • •Server provisioning input validation

Related patterns