^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/(?:\d{2})?\d{2}$
Kopieren
This regex pattern matches dates in the format DD/MM/YYYY or DD/MM/YY. It allows for days from 01 to 31, months from 01 to 12, and optionally matches the year in either two-digit (YY) or four-digit (YYYY) format.
const regex = /^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/(?:\d{2})?\d{2}$/;
const date = '25/12/2021';
if(regex.test(date)) {
console.log('Valid date format');
} else {
console.log('Invalid date format');
}
Kopieren