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 = [];

while (match = re.exec(str))
    results.push(+match[1]);



document.write('<pre>' + 
               JSON.stringify(results, null, 4) +
               '</pre>')