const regex = /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-9])|(2[0-3]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])))?))?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^((\\d{2}(([02468][048])|([13579][26]))[\\-\\\/\\s]?((((0?[13578])|(1[02]))[\\-\\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\\/\\s]?((((0?[13578])|(1[02]))[\\-\\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[1-9])|(1[0-9])|(2[0-3]))\\:([0-5][0-9])((\\s)|(\\:([0-5][0-9])))?))?$', 'gm')
const str = `16/10/1991
16-10-1991
16.10.1991
1991/16/10
1991-16-10
1991.16.10
10/16/1991
10-16-1991
10.16.1991
1991-10-16
1991/10/16
2016-2-29
2020-2-29
2024-2-29
2028-2-29
2032-2-29
2036-2-29
2040-2-29
5304-2-29
1991.10.16
91-10-16
91/10/16
91.10.16
01/01/2000
31/01/2000
32/01/2000
01/1/2000
01/1/01
29/02/2000
28/02/2001
29/02/2003
30/04/2000
31/04/2000
31/07/2000
31/08/2000
31/09/2000
01/12/2000
01/15/2000
`;
// 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