JS REGEX 06: JavaScript’s Secret Finders — .test(), .exec(), and .match()
Imagine you’re a word detective, holding three special magnifying glasses. Each glass helps you find hidden words or letters inside sentences — but each one works a little differently.
Today, we’ll meet our three heroes:
- 🕵️
.test()
— the quick yes/no detective - 🧠
.exec()
— the deep-diving investigator - 🔍
.match()
— the friendly helper that works from the string side
Ready? Let’s investigate! 🚀
🧪 1. .test()
– “Is it there?”
Sometimes, you just want a simple answer: 👉 “Does this word appear in the sentence or not?”
That’s where .test()
comes in!
var re = /cat/;
console.log(re.test("The cat is here")); // true
console.log(re.test("No animal here")); // false
.test()
gives you a boolean:
true
→ if it finds a matchfalse
→ if it doesn’t
It’s the quickest of the three — no extra info, no fuss. Think of it like asking a guard: “Do you see a cat here?” He simply nods yes or shakes his head no. 🐈
📦 2. .exec()
– “Tell me everything!”
Now, let’s say you don’t just want to know if the word is there — you also want to know where it was found and what part matched.
That’s what .exec()
does. It gives you a whole report!
var re = /c(at)/;
var result = re.exec("The cat is here");
console.log(result[0]); // "cat" → the full match
console.log(result[1]); // "at" → captured group
console.log(result.index); // 4 → position in the sentence
See how .exec()
gives details? It’s like a detective who brings back:
“I found the word ‘cat’ at position 4, and I also noticed the letters ‘at’ inside it!”
It even works beautifully with loops when used with /g
for global searches.
It’s precise, methodical, and very detailed — perfect for pattern detectives.
🔁 3. .match()
– “Search it from the string side!”
Finally, .match()
is a bit like .exec()
— but instead of being called from the regex, it’s called from the string itself!
Let’s see it in action:
var sentence = "The cat is here";
var result = sentence.match(/cat/);
console.log(result[0]); // "cat"
Nice and simple!
But when you want to find all matches, you can use the g
flag:
var str = "cat, bat, hat";
console.log(str.match(/at/g)); // ["at", "at", "at"]
So .match()
is your go-to tool when you just want a list of all the matches quickly.
📚 Summary – The Detective Trio
🔍 Goal | 🧰 Tool to Use | 🪄 What It Returns |
---|---|---|
Just check if there’s a match | .test() |
true or false |
Get full match info and groups | .exec() |
Array with full match + details |
Find matches from a string easily | .match() |
Single match or array of matches |
Each one is powerful — but the real magic happens when you know which to use when! 🌟
💡 Quick Analogy
Imagine you’re at a treasure hunt:
.test()
just says, “Yes, treasure is here!”.exec()
digs it up and gives you the coordinates!.match()
lists all treasures in one glance!
That’s how regex turns you into a text-finding superhero! 🦸♀️🦸♂️
🧩 Review – Fill in the Gaps
Now, let’s check how sharp your detective skills are! Fill in the missing words to complete these sentences. ✍️
- The
.test()
method returns either______
or______
. .exec()
provides detailed match information like the full match, captured groups, and the match______
.- To search for all matches, you add the
______
flag to your regular expression. - The method
.match()
is called on the______
, not the regex. .test()
is best when you only need a simple______
or______
answer.- The captured parts of a regex can be accessed inside
.exec()
using bracket notation likeresult[______]
. - When
.exec()
finds a match, it returns an array; if no match, it returns______
. - The
.match()
method can return an______
of all found matches when used with the global flag. - The property
result.index
tells you where the______
started in the string. - All three —
.test()
,.exec()
, and.match()
— are used to______
for patterns inside strings.