Cache-Control Directive Regex for JavaScript
/^(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10})(?:\s*,\s*(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10}))*$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching cache-control directive, 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
// Cache-Control Directive
// ReDoS-safe | RegexVault — Web & Network > HTTP
const cachecontrolDirectiveRegex = /^(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10})(?:\s*,\s*(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10}))*$/i;
function validateCachecontrolDirective(input: string): boolean {
return cachecontrolDirectiveRegex.test(input);
}
// Example
console.log(validateCachecontrolDirective("no-cache")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
no-cache | max-age=abc |
max-age=3600 | invalid-directive |
public, max-age=86400 | cache-control |
no-store, no-cache | max-age=-1 |
max-age=0, must-revalidate | max-age= |
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
Cache-Control directives are space-tolerant after commas but must not have spaces around = signs. The pattern enforces no spaces around =.
Technical Notes
max-age values are in seconds. max-age=0 combined with must-revalidate is equivalent to no-cache for strict clients. Stale-while-revalidate is from RFC 5861.
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