Categories
JavaScript Answers

How to check if a string contains text from an array of substrings in JavaScript?

Spread the love

To check if a string contains text from an array of substrings in JavaScript, we can use the some and includes method.

For instance, we write

if (substrings.some((v) => str.includes(v))) {
  // ...
}

to call some on the substrings array with a callback that calls str.includes with the v entry being looped through in substrings to check if v is in the str string.

If any value v is in str, then some returns true.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.