const regex = /^z[0-3|6-8]{1}$|^z5(ab|c)$|^z12c$|^z1[5-7]$|^z20$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^z[0-3|6-8]{1}$|^z5(ab|c)$|^z12c$|^z1[5-7]$|^z20$', 'gm')
const str = `z0
z1
z2
z3
z4
z5
z5a
z5b
z5ab
z5c
z6
z7
z8
z9
z10
z11
z12
z12c
z13
z14
z15
z16
z17
z18
z19
z20`;
// 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