const regex = /^(?<year>\d{4})(?:(?<month>\d{2})(?:(?<day>\d{2})(?:(?<hour>\d{2})(?:(?<minute>\d{2})(?:(?<second>\d{2})(?:.(?<millis>\d{3})(?:\[(?:(?<offsetSign>[+-]?)(?<offsetHour>\d{1,2}))(?:.(?<offsetMin>\d{2}))?(?::(?<tz>.*)?)?\])?)?)?)?)?)?)?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<year>\\d{4})(?:(?<month>\\d{2})(?:(?<day>\\d{2})(?:(?<hour>\\d{2})(?:(?<minute>\\d{2})(?:(?<second>\\d{2})(?:.(?<millis>\\d{3})(?:\\[(?:(?<offsetSign>[+-]?)(?<offsetHour>\\d{1,2}))(?:.(?<offsetMin>\\d{2}))?(?::(?<tz>.*)?)?\\])?)?)?)?)?)?)?$', 'gm')
const str = `1996
199610
19961005
1996100513
199610051322
19961005132200
19961005132200.124
19961005132200.124[-5]
19961005132200.124[-5:EST]
19961005132200.124[-5.30:EST]
19961005132200.124[-5.30:]
19961005132200.124[-5.30]
19961005132200.124[+5]
19961005132200.124[+5.30]
19961005132200.124[+5:EST]
19961005132200.124[+5.30:EST]
19961005132200.124[+5.30:]
19961005132200.124[5]
19961005132200.124[5.30]
19961005132200.124[5:EST]
19961005132200.124[5.30:EST]
19961005132200.124[5.30:]
`;
// 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