If it is about the longest possible matching (sub)string, thus the most specific one, the task actually can be solved pretty easy.
- sortthe array descending by each of its string-item’s- lengthproperty.
- Iterate the array … for each string item, try whether it is included within the given text.
- Stop iterating with the first matching string-item.
No need of regex at all.
function getMostSpecificMatch(text, matchList) {
  let match;
  matchList
    .sort((a, b) => b.length - a.length)
    .some(str => {
      const doesMatch = text.includes(str);
      if (doesMatch) {
        match = str;
      }
      return doesMatch;
    });
  return match;
}
const sampleText="Hello Teddy Bear, enjoy your life";
const sampleList = ['Teddy', 'Teddy Bear'];
console.log(
  'randomly ordered list of possible matches ...',
  sampleList
);
console.log(
  "possible matches in descending order of each item's length ...",
  sampleList.sort((a, b) => b.length - a.length)
);
console.log({ sampleText });
console.log(
  'most specific match ...',
  getMostSpecificMatch(sampleText, sampleList)
);.as-console-wrapper { min-height: 100%!important; top: 0; }2
solved find and differentiate words in javascript [closed]