JavaScript / ECMAScript

Hostname (Single Label, RFC 1123) in JS

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

Try it in the JS tester →

Pattern

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

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 matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern 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

Same pattern, other engines

← Back to Hostname (Single Label, RFC 1123) overview (all engines)