Regex to Match Cron Expression Format
Validate standard 5-field cron expression format with fields for minute, hour, day, month, and weekday. Supports wildcards, ranges, and steps. Free regex tester.
Regular Expression
/^(?:[0-9*,/\-]+\s+){4}[0-9*,/\-]+$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| (?: | Start of non-capturing group |
| [0-9*,/\-] | Character class — matches any one of: 0-9*,/\- |
| + | Matches the preceding element one or more times (greedy) |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| {4} | Matches exactly 4 times |
| [0-9*,/\-] | Character class — matches any one of: 0-9*,/\- |
| + | Matches the preceding element one or more times (greedy) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates the structural format of standard 5-field cron expressions. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
(?:[0-9*,/-]+\s+){4} — A non-capturing group that matches exactly four cron fields, each followed by whitespace. Each field consists of one or more characters from the cron character set: digits (0-9) for specific values, asterisk (*) for wildcards meaning every value, comma (,) for lists of values, forward slash (/) for step values, and hyphen (-) for ranges. The \s+ matches one or more spaces separating the fields. The {4} quantifier requires exactly four such field-plus-space sequences.
[0-9*,/-]+ — Matches the fifth and final cron field (day of week) using the same character set but without trailing whitespace.
$ — Anchors the match at the end of the string.
No flags are used since this validates a single cron expression string.
The five fields in a standard cron expression represent: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7). Examples include: * * * * * (every minute), 0 9 * * 1-5 (9 AM on weekdays), */5 * * * * (every 5 minutes), and 0 0 1 1 * (midnight on January 1st).
This pattern validates the structural format but does not validate the semantic correctness of field values. It is useful for input validation in cron builders, scheduling interfaces, and configuration file parsers.
Example Test Strings
| Input | Expected |
|---|---|
| * * * * * | Match |
| 0 9 * * 1-5 | Match |
| */5 * * * * | Match |
| not a cron | No Match |
| * * * | No Match |
| 0,30 * * * * | Match |
Try It — Interactive Tester
34 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match ISO 8601 Duration Format
/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(?:T(?!$)(\d+H)?(\d+M)?(\d+(?:\.\d+)?S)?)?$/
Regex to Match Relative Date Expressions
/(\d+)\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now|later|earlier)/gi
Regex to Match 24-Hour Time Format
/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/
Regex to Match Integers
/^-?\d+$/