const regex = /\w+ \((?:[1-9]|[1234][0-9])\)(?:, )?/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\w+ \\((?:[1-9]|[1234][0-9])\\)(?:, )?', 'gm')
const str = `I can not find a working regular expression to match the strings followed by a number from 1 to 49 in braces, in a list dividing the pairs by comma:
alpha (1), beta (12), gamma (37), delta (49), epsilon (55), zeta (64)
but also in random order and amount of pairs like
zeta (64), alpha (1), gamma (37), beta (12), delta (49), epsilon (55), eta (48), theta (26)
and also random numbers like
zeta (11), gamma (71), beta (49), delta (52), epsilon (99), alpha (33)
I'm looking for an endresult showing only the pairs with a number lower as 49 like
alpha (1), beta (12), gamma (37), delta (49)
alpha (1), gamma (37), beta (12), delta (49), eta (48), theta (26)
zeta (11), beta (49), alpha (33)`;
// 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