const regex = /^XXX-\d{4}-((?![XYT])[A-Z]){4}$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^XXX-\\d{4}-((?![XYT])[A-Z]){4}$', 'gm')
const str = `XXX-0000-XBCD
XXX-0000-AXCD
XXX-0000-ABXD
XXX-0000-ABCX
XXX-0000-XXCD
XXX-0000-XBXD
XXX-0000-XBCX
XXX-0000-AXXD
XXX-0000-AXCX
XXX-0000-ABXX
XXX-0000-XXXX
XXX-0000-AB1D
XXX-0000-A%CD
XXX-0000-ABCDE
XXX-0000-ABCDX
AXXX-0000-ABCD
XXX-A0000-ABCD
XXX-0000-ABCD
`;
// 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