Categories
JavaScript Answers

How to match string against the array of regular expressions with JavaScript?

Spread the love

To match string against the array of regular expressions with JavaScript, we use the some method.

For instance, we write

const regexList = [/apple/, /pear/];
const text = "banana pear";
const isMatch = regexList.some((rx) => rx.test(text));

to call regexList.some with a callback that calls rx.test with text to check if text matches any of the regexes in regexList.

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 *