const regex = /\(20(\d{2}|x{2})((\.|-)([0-9]{2}|x{2}|KW(\d{2}|x{2})|Q(x|[1-4])|(([0-9]{2}|x{2})(\.|-)([0-9]{2}|x{2}))))?\)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\(20(\\d{2}|x{2})((\\.|-)([0-9]{2}|x{2}|KW(\\d{2}|x{2})|Q(x|[1-4])|(([0-9]{2}|x{2})(\\.|-)([0-9]{2}|x{2}))))?\\)', 'g')
const str = `20000002000
200 0
(2022.xx2000.xx)
(2022.xx) 2000.xx
(2020-20)
(20xx-12)
(20xx.xx-11)
(2002-KW00.00)
(2002-KW00)
(2022.KWxx)
(2021.KW09)
(2021.KW09)
(2021.02)
2022.Qx
2032.Q4
(2020.xx)
2022-Q1
2022 Test
Test 20xx
20202020
.Qx
KW23
KWxx
(20xx)
(.-Q4)
2000-
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