const regex = new RegExp('\\b004010H2[127][02379](?:A[12])?\\b', 'g')
const str = `\$str = "this is the code 004010H222A1 the rest is irrelevant";
\$str = "the random number is 004010H223A2 ** anything else is irrelevant";
\$str = "the last lottery number 004010H220A1 ~~ the rest is irrelevant";
\$str = "yet another random sentence 004010H279A1 the rest is irrelevant";
\$str = "any sentence before what i want 004010H279A1 the rest is irrelevant";
\$str = "last winning number 004010H217~~~";
`;
// 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