Validation

BCP 47 Language Tag

Validate BCP 47 language tags like `en`, `en-US`, `zh-Hant-TW`, or `pt-BR`.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^[a-zA-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$", "");
const input = "en";
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-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$

Raw source: ^[a-zA-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$

How it works

^[a-zA-Z]{2,3} matches the primary 2- or 3-letter language subtag. (?:-[A-Za-z0-9]{2,8})* matches subsequent subtags (script, region, variant) joined with hyphens. Each subtag is 2–8 alphanumerics. This validates STRUCTURE — for spec-compliant validation use a real BCP 47 parser.

Examples

Input

en

Matches

  • en

Input

zh-Hant-TW

Matches

  • zh-Hant-TW

Input

ENGLISH

No match

—

Common use cases

  • •i18n routing in web frameworks
  • •Accept-Language header validation
  • •CMS locale config
  • •Translation pipeline input filtering

Related patterns