Logo

How to check whether a string matches a regex in JS?

In JavaScript, the simplest way to see if a string matches a regular expression is to use the test() method of a RegExp object. It returns a boolean indicating whether the pattern matches or not. Below are a few examples and additional ways to check matches:

1. Using RegExp.prototype.test()

const pattern = /hello/; // Regular expression const str = 'Hello world'; pattern.test(str); // false (case-sensitive) pattern.test(str.toLowerCase()); // true (if you convert str to lowercase)
  • test(str) returns true if str contains a match for pattern, else false.

Example with Flags

const pattern = /hello/i; // The 'i' flag makes it case-insensitive const str = 'Hello world'; console.log(pattern.test(str)); // true

2. Using String.prototype.match()

You can also call match() on the string if you want to retrieve the matched substring(s) rather than just check if it exists:

const pattern = /hello/i; const str = 'Hello world'; const result = str.match(pattern); if (result) { console.log('Match found:', result[0]); } else { console.log('No match found'); }
  • match(pattern) returns an array of matches or null if no match exists.
  • For repeated matches, include the g (global) flag in your regex to get all matches.

3. matchAll() for Multiple Matches (ES2020+)

If you need all matches (including capturing groups) in an iterable format:

const pattern = /(\w+)@(\w+\.\w+)/g; const str = 'Emails: person@example.com, contact@test.org'; for (const match of str.matchAll(pattern)) { console.log('Full match:', match[0]); console.log('Group 1:', match[1]); // The part before '@' console.log('Group 2:', match[2]); // The domain part }
  • Returns an iterator of matches (each match is an array).
  • Great for iterating over multiple matches with capturing groups.

4. Summary of Approaches

  1. test()

    • Best for a true/false check.
    • pattern.test(str) // => boolean.
  2. match()

    • Good for retrieving the matched substring or an array of matches (if g flag is used).
    • str.match(pattern) // => array or null.
  3. matchAll() (ES2020+)

    • Ideal when you need an iterator over all matches, including sub-groups.
    • str.matchAll(pattern) // => iterator of match arrays.

Bonus: Strengthen Your JavaScript and Coding Skills

If you want to dive deeper into JavaScript fundamentals and elevate your coding interview skills, check out these courses from DesignGurus.io:

  1. Grokking JavaScript Fundamentals
    Gain a solid understanding of closures, prototypes, async/await, and other critical JS concepts.

  2. Grokking the Coding Interview: Patterns for Coding Questions
    Master common coding patterns needed for technical interviews and real-world problem solving.

For personalized feedback, explore their Mock Interview services:

Also, find free tutorials on the DesignGurus.io YouTube channel.

Final Thoughts

  • To check if a string matches a regex in JS: pattern.test(str) (returns boolean) or str.match(pattern) (returns array or null).
  • Which method you choose depends on whether you merely need a yes/no answer (test()), the actual match data (match(), matchAll()), or deeper capturing group details for more complex tasks.
CONTRIBUTOR
TechGrind