Accept-Language Header Value Regex for JavaScript
/^[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?(?:\s*,\s*[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?)*$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching accept-language header value, ported and verified for JavaScript. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// Accept-Language Header Value
// ReDoS-safe | RegexVault — Web & Network > HTTP
const acceptlanguageHeaderValueRegex = /^[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?(?:\s*,\s*[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?)*$/i;
function validateAcceptlanguageHeaderValue(input: string): boolean {
return acceptlanguageHeaderValueRegex.test(input);
}
// Example
console.log(validateAcceptlanguageHeaderValue("en")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
en | en;q=1.5 |
en-US | en;q=-0.1 |
en-US,en;q=0.9,fr;q=0.8 | 123 |
zh-CN,zh;q=0.9,en;q=0.8 | en-US, fr;q=abc |
de-CH-x-phonebk | en-US;;q=0.9 |
When to use this pattern
This pattern is drawn from the Web & Network > HTTP category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
An empty Accept-Language value is not valid — some clients send * (any language) as a wildcard, which this pattern does not match. Add \* as an additional alternative if needed.
Technical Notes
Quality factor (q) must be between 0 and 1 with up to 3 decimal places. Default q value is 1.0 when omitted. Used for content negotiation.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern