JavaScript / ECMAScript

BCP 47 Language Tag in JS

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

Try it in the JS tester →

Pattern

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

JavaScript / ECMAScript code

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

Same pattern, other engines

← Back to BCP 47 Language Tag overview (all engines)