JSFiddle - React, Tailwind, and code Playground
JavaScript
var text = "DC: Batgirl, Marvel: Rogue, Image: Spawn, Image: Celestine, Marvel: Iron Man, DC: Superman, Image: Ant",
brandRegexp = /(^|, )(\S+):/g,
regexp, brands, res, heroes, brand;
brands = {};
while (res = brandRegexp.exec(text)) {
brand = res[2];
brands[brand] ? brands[brand]++ : brands[brand] = 1;
}
for (brand in brands) {
regexp = new RegExp(brand + ": ([\\S ]+?)(,|$)","g");
heroes = [];
while (res = regexp.exec(text)) {
heroes.push(res[1]);
}
console.log("Found", brands[brand], "heroes for", brand, "comics:", heroes.join(", "));
}