JSFiddle - React, Tailwind, and code Playground
JavaScript
var re = /foo_(\d+)/g,
str = "text foo_123 more text foo_456 foo_789 end text",
match,
results = [];
do {
match = re.exec(str);
if (match)
results.push(+match[1]);
} while (match);
document.write('<pre>' +
JSON.stringify(results, null, 4) +
'</pre>')