const regex = new RegExp('(?:(?P<Day1>\\d{1,2})(?:\\W*(?P<Month1>[A-Z][a-z]*\\b))|(?:(?P<Month2>[A-Z][a-z]*\\b)\\W*)(?:(?P<Day2>\\d{1,2})(?:\\b|th|st|nd))?|(?P<Month3>\\d{1,2})\\W+(?P<Day3>\\d{1,2})|(?P<Month4>\\d{1,2}))?\\W+?(?P<Year>\\d{2,4}(?=$|;))', 'gm')
const str = `04/20/2009
04/20/09
4/20/09
4/3/09
Mar-20-2009
Mar 20, 2009
March 20, 2009
Mar. 20, 2009
Mar 20 2009;
20 Mar 2009
20 March 2009
20 Mar. 2009
20 March, 2009
Mar 20th, 2009
Mar 21st, 2009
Mar 22nd, 2009
Feb 2009
Sep 2009
Oct 2010
6/2008
12/2009
2009
2010`;
// 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