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
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).
Python (re) code
import re
pattern = re.compile(r"^[a-zA-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$")
input_text = "en"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$`)
input := `en`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^[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
Examples
Input
enMatches
en
Input
zh-Hant-TWMatches
zh-Hant-TW
Input
ENGLISHNo match
—Common use cases
- •i18n routing in web frameworks
- •Accept-Language header validation
- •CMS locale config
- •Translation pipeline input filtering
Related patterns
MIME Type
ValidationValidate MIME media type strings like text/html, application/json, or image/png; charset=utf-8.
Cron Expression
ValidationValidate standard 5-field Unix cron expressions: minute, hour, day-of-month, month, day-of-week.
Cron Expression (Quartz/Spring, 6-Field)
ValidationValidate 6-field Quartz/Spring cron expressions including the seconds field at the front.
Email Address Validation
ValidationMatch and validate email addresses in the standard user@domain.tld format.