const regex = /.*?\((.*)%\)|^(\S+)\s\[.*/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('.*?\\((.*)%\\)|^(\\S+)\\s\\[.*', 'gm')
const str = `1991 (38.26%)
1018 (41.52%)
438 (35.12%)
771 (35.16%)
244 (35.72%)
343 (32.48%)
316 (40.51%)
177 (33.84%)
133 (41.18%)
792 (35.92%)
3190 (61.30%)
1426 (58.16%)
803 (64.39%)
1415 (64.52%)
436 (63.84%)
711 (67.33%)
463 (59.36%)
345 (65.97%)
187 (57.89%)
1403 (63.63%)
44.00 [38.00 - 50.00]
43.00 [37.00 - 49.00]
43.00 [37.00 - 49.00]
44.00 [38.00 - 50.00]
44.00 [39.00 - 50.00]
44.00 [38.00 - 50.00]
43.00 [37.00 - 49.00]
45.00 [39.00 - 51.00]
44.00 [37.00 - 50.00]
45.00 [38.00 - 51.00]
`;
// 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