JSFiddle - React, Tailwind, and code Playground
JavaScript
text = "My name is Eric. Eric lives in New York Eriic";
var myName = "Eric";
var hits = [];
var pos = text.indexOf(myName);
while (pos > -1) {
for (var j = pos; j < (myName.length + pos); j++) {
hits.push(text[j]);
}
pos = text.indexOf(myName, pos + 1);
}
(hits.length === 0) ? console.log("Your name wasn't found!") : console.log(hits);