const regex = /99_\w{3}_(A|B)_((X|(?>YZ))(0*[1-9][0-9]?)_(FOO|BAR) \3Y?-(\b0?20(1[4-9]|[2-9][0-9])\b)-\b0?((?:[1-9]|1[0-2])|(?:[4]|1[0]))\b$)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('99_\\w{3}_(A|B)_((X|(?>YZ))(0*[1-9][0-9]?)_(FOO|BAR) \\3Y?-(\\b0?20(1[4-9]|[2-9][0-9])\\b)-\\b0?((?:[1-9]|1[0-2])|(?:[4]|1[0]))\\b$)', 'gm')
const str = `99_GOG_A_X1_FOO X-2014-09
99_YAK_A_YZ1_BAR YZY-2014-10
99_YAK_A_YZ1_BAR YZY-2014-11
99_GOG_A_X1_FOO X-2014-13
99_YAK_A_X1_BAR YZY-2014-10
99_GOG_A_YZ1_FOO X-2014-10
99_GOG_A_X1_FOO X-2014-11`;
// 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