const regex = /\W\n?(((1|2)[0-9]|3[0-1]|0?[1-9])\/(0?[1-9]|1[0-2])\/((19|20)?[0-9]{2})|((1|2)[0-9]|3[0-1]|0?[1-9])\-(0?[1-9]|1[0-2])\-((19|20)?[0-9]{2})|((19|20)?[0-9]{2})\/((1|2)[0-9]|3[0-1]|0?[1-9])\/(0?[1-9]|1[0-2])|((19|20)?[0-9]{2})\-((1|2)[0-9]|3[0-1]|0?[1-9])\-(0?[1-9]|1[0-2]))\W\n?/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\W\\n?(((1|2)[0-9]|3[0-1]|0?[1-9])\\\/(0?[1-9]|1[0-2])\\\/((19|20)?[0-9]{2})|((1|2)[0-9]|3[0-1]|0?[1-9])\\-(0?[1-9]|1[0-2])\\-((19|20)?[0-9]{2})|((19|20)?[0-9]{2})\\\/((1|2)[0-9]|3[0-1]|0?[1-9])\\\/(0?[1-9]|1[0-2])|((19|20)?[0-9]{2})\\-((1|2)[0-9]|3[0-1]|0?[1-9])\\-(0?[1-9]|1[0-2]))\\W\\n?', 'gm')
const str = ` 12-01-2019
01/10/2019
1-12-1901
04/04/2019
2000-02-12
2000/10/12
1800-1-9
1976/3/1
32/01/3019
9/1/30
5542-21-54
`;
// 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