const regex = /^(\w+\s*=|(\*(\/\d+)?|([0-5]?\d)(-([0-5]?\d)(\/\d+)?)?(,([0-5]?\d)(-([0-5]?\d)(\/\d+)?)?)*)\s+(\*(\/\d+)?|([01]?\d|2[0-3])(-([01]?\d|2[0-3])(\/\d+)?)?(,([01]?\d|2[0-3])(-([01]?\d|2[0-3])(\/\d+)?)?)*)\s+(\*(\/\d+)?|(0?[1-9]|[12]\d|3[01])(-(0?[1-9]|[12]\d|3[01])(\/\d+)?)?(,(0?[1-9]|[12]\d|3[01])(-(0?[1-9]|[12]\d|3[01])(\/\d+)?)?)*)\s+(\*(\/\d+)?|([1-9]|1[012])(-([1-9]|1[012])(\/\d+)?)?(,([1-9]|1[012])(-([1-9]|1[012])(\/\d+)?)?)*|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+(\*(\/\d+)?|([0-7])(-([0-7])(\/\d+)?)?(,([0-7])(-([0-7])(\/\d+)?)?)*|mon|tue|wed|thu|fri|sat|sun)|(@reboot|@yearly|@annually|@monthly|@weekly|@daily|@midnight|@hourly))$/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(\\w+\\s*=|(\\*(\\\/\\d+)?|([0-5]?\\d)(-([0-5]?\\d)(\\\/\\d+)?)?(,([0-5]?\\d)(-([0-5]?\\d)(\\\/\\d+)?)?)*)\\s+(\\*(\\\/\\d+)?|([01]?\\d|2[0-3])(-([01]?\\d|2[0-3])(\\\/\\d+)?)?(,([01]?\\d|2[0-3])(-([01]?\\d|2[0-3])(\\\/\\d+)?)?)*)\\s+(\\*(\\\/\\d+)?|(0?[1-9]|[12]\\d|3[01])(-(0?[1-9]|[12]\\d|3[01])(\\\/\\d+)?)?(,(0?[1-9]|[12]\\d|3[01])(-(0?[1-9]|[12]\\d|3[01])(\\\/\\d+)?)?)*)\\s+(\\*(\\\/\\d+)?|([1-9]|1[012])(-([1-9]|1[012])(\\\/\\d+)?)?(,([1-9]|1[012])(-([1-9]|1[012])(\\\/\\d+)?)?)*|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s+(\\*(\\\/\\d+)?|([0-7])(-([0-7])(\\\/\\d+)?)?(,([0-7])(-([0-7])(\\\/\\d+)?)?)*|mon|tue|wed|thu|fri|sat|sun)|(@reboot|@yearly|@annually|@monthly|@weekly|@daily|@midnight|@hourly))$', 'g')
const str = `0 12-20 * * 4`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions