JSFiddle - React, Tailwind, and code Playground
CSS
body { font-family: Consolas, monospace; white-space: pre; }
JavaScript
var str = "Cars: 5\
Fruits: 2\
Cars: 1\
Carrier: 20\
Cars: 20\
Hey: 2";
var arr = [];
str.replace(/Cars:\s(\d+)/g, function ($0, num) {
arr.push(+num);
});
document.body.innerHTML = JSON.stringify(arr, null, "\t");