const regex = /^((((r?d_(MON|TUE|WED|THU|FRI|SAT|SUN))_?([0][1-5])?(([-+])([0-9]{3}))?)|(r?d_[0-9]{3}))|rd|((r?m_((JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|([0][1-9]|[1][0-2])))_?((d[0-9]{2})|(MON|TUE|WED|THU|FRI|SAT|SUN)_?([0][1-5])?)))$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^((((r?d_(MON|TUE|WED|THU|FRI|SAT|SUN))_?([0][1-5])?(([-+])([0-9]{3}))?)|(r?d_[0-9]{3}))|rd|((r?m_((JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|([0][1-9]|[1][0-2])))_?((d[0-9]{2})|(MON|TUE|WED|THU|FRI|SAT|SUN)_?([0][1-5])?)))$', 'gm')
const str = `d_MON_02
rd
rd_029
rd_MON
rd_MON_01
rd_MON_01+007
rd_WED_03-007
rm_JAN_d29
rm_01_d29
rm_JAN_MON02
m_JAN_MON01
rm_JAN_MON
one off second Monday
repeat daily
repead daily on the 29th
repead daily on Monday
repeat first Monday
repeat second Monday with offset of 7 days
repeat second Wednesday with negative offset of 7 days
repeat on 29th of January with 3 characters month name format
repeat on 29th of January with 2 digit numbering month format
repeat second Monday of January
one off first Monday of January
repeat all Mondays in January`;
// 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