Cron Expression (6-Field Quartz/Spring) Regex for JavaScript
/^((?:[0-5]?[0-9]|\*)(?:[/,](?:[0-5]?[0-9]|\*))*) ((?:[0-5]?[0-9]|\*)(?:[/,](?:[0-5]?[0-9]|\*))*) ((?:[01]?[0-9]|2[0-3]|\*)(?:[/,](?:[01]?[0-9]|2[0-3]|\*))*) ((?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*)(?:[/,](?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*))*) ((?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*)(?:[/,](?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*))*) ((?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?])(?:[-/,](?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?]))*)$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching cron expression (6-field quartz/spring), 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
// Cron Expression (6-Field Quartz/Spring)
// ReDoS-safe | RegexVault — Dev & Systems > Cron
const cronExpression6fieldQuartzspringRegex = /^((?:[0-5]?[0-9]|\*)(?:[\/,](?:[0-5]?[0-9]|\*))*) ((?:[0-5]?[0-9]|\*)(?:[\/,](?:[0-5]?[0-9]|\*))*) ((?:[01]?[0-9]|2[0-3]|\*)(?:[\/,](?:[01]?[0-9]|2[0-3]|\*))*) ((?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*)(?:[\/,](?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*))*) ((?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*)(?:[\/,](?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*))*) ((?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?])(?:[-\/,](?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?]))*)$/i;
function validateCronExpression6fieldQuartzspring(input: string): boolean {
return cronExpression6fieldQuartzspringRegex.test(input);
}
// Example
console.log(validateCronExpression6fieldQuartzspring("0 * * * * ?")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
0 * * * * ? | * * * * * |
0 0 12 * * ? | 0 0 25 * * ? |
0 15 10 ? * MON-FRI | 0 0 12 * * 8 |
0 0/5 14 * * ? | 60 * * * * ? |
0 0 0 L * ? | abc def ghi |
When to use this pattern
This pattern is drawn from the Dev & Systems > Cron 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
Spring's @Scheduled uses 6-field Quartz syntax. AWS CloudWatch Events uses EventBridge cron which differs from both.
Technical Notes
Quartz adds a seconds field at position 1 and supports L (last), W (weekday nearest), and # (nth weekday) modifiers. Named months and days are case-insensitive with the i flag.
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