Вы можете использовать объект RegExp
для выражений c, а также функции map
и reduce
для подсчета.
let names= ["jhon", "parker"],
sentences = ["hello jhon", "hello parker and parker", "jhonny jhonny yes parker"],
result = names.map(n => sentences.reduce((a, s) => a + (s.match(new RegExp(`\\b${n}\\b`, "g")) || []).length, 0));
console.log(result);
Подход линейной сложности
let names= ["jhon", "parker"],
sentences = ["hello jhon", "hello parker and parker", "jhonny jhonny yes parker"],
words = sentences.join(" "),
result = names.map(n => (words.match(new RegExp(`\\b${n}\\b`, "g")) || []).length);
console.log(result);