Categories
JavaScript Answers

How to check for multiple string matches with JavaScript?

Spread the love

Sometimes, we want to check for multiple string matches with JavaScript.

In this article, we’ll look at how to check for multiple string matches with JavaScript.

How to check for multiple string matches with JavaScript?

To check for multiple string matches with JavaScript, we can use the JavaScript string’s match method with a regex.

For instance, we write:

const str = 'audio video'
if (str.match(/(video|audio)/)) {
  console.log('found')
} else {
  console.log('not found')
}

to call str.match with a regex.

Since 'audio' and 'video' are both in str, str.match(/(video|audio)/) returns true.

Conclusion

To check for multiple string matches with JavaScript, we can use the JavaScript string’s match method with a regex.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *