const regex = /(((20[012]\d|19\d\d)|(1\d|2[0123]))-((0[0-9])|(1[012]))-((0[1-9])|([12][0-9])|(3[01])))|(((0[1-9])|([12][0-9])|(3[01]))-((0[0-9])|(1[012]))-((20[012]\d|19\d\d)|(1\d|2[0123])))|(((20[012]\d|19\d\d)|(1\d|2[0123]))\/((0[0-9])|(1[012]))\/((0[1-9])|([12][0-9])|(3[01])))|(((0[0-9])|(1[012]))\/((0[1-9])|([12][0-9])|(3[01]))\/((20[012]\d|19\d\d)|(1\d|2[0123])))|(((0[1-9])|([12][0-9])|(3[01]))\/((0[0-9])|(1[012]))\/((20[012]\d|19\d\d)|(1\d|2[0123])))|(((0[1-9])|([12][0-9])|(3[01]))\.((0[0-9])|(1[012]))\.((20[012]\d|19\d\d)|(1\d|2[0123])))|(((20[012]\d|19\d\d)|(1\d|2[0123]))\.((0[0-9])|(1[012]))\.((0[1-9])|([12][0-9])|(3[01])))/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(((20[012]\\d|19\\d\\d)|(1\\d|2[0123]))-((0[0-9])|(1[012]))-((0[1-9])|([12][0-9])|(3[01])))|(((0[1-9])|([12][0-9])|(3[01]))-((0[0-9])|(1[012]))-((20[012]\\d|19\\d\\d)|(1\\d|2[0123])))|(((20[012]\\d|19\\d\\d)|(1\\d|2[0123]))\\\/((0[0-9])|(1[012]))\\\/((0[1-9])|([12][0-9])|(3[01])))|(((0[0-9])|(1[012]))\\\/((0[1-9])|([12][0-9])|(3[01]))\\\/((20[012]\\d|19\\d\\d)|(1\\d|2[0123])))|(((0[1-9])|([12][0-9])|(3[01]))\\\/((0[0-9])|(1[012]))\\\/((20[012]\\d|19\\d\\d)|(1\\d|2[0123])))|(((0[1-9])|([12][0-9])|(3[01]))\\.((0[0-9])|(1[012]))\\.((20[012]\\d|19\\d\\d)|(1\\d|2[0123])))|(((20[012]\\d|19\\d\\d)|(1\\d|2[0123]))\\.((0[0-9])|(1[012]))\\.((0[1-9])|([12][0-9])|(3[01])))', 'gm')
const str = `work:
2022-01-01
01-01-2020
1990/01/01
12/30/2020
30/12/2020
30.12.2020
2020.12.30
19-12-30
30-12-19
2020/12/30
12/30/19
30/12/19
30.12.19
19.12.30
not work:
1022-01-01
01-15-2020
2990/01/01
13/30/2020
30/13/2020
30.12.9029
2020.14.30
2020.12.33
19-12-33
19-13-30
33-12-19
30-12-35
2020/13/30
2020/12/34
12/30/29
30/12/29
30.12.26
30.12.30
13/30/19
30/13/19
30.13.19
19.13.30
12/33/19
33/12/19
33.12.19
19.12.33
1/1/19
1/01/19
01/1/19
1/01/2019
1/1/2019
01/1/2019
`;
// 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