JS REGEX 06: JavaScript’s Secret Finders — .test(), .exec(), and .match()

When working with text or user input in JavaScript, Regular Expressions (RegEx) help you search, match, and extract specific patterns from strings.
But to perform these searches effectively, you need the right RegEx methods — and that’s where .test(), .exec(), and .match() come in.
Although these three methods appear similar, they serve distinct purposes and return different types of results. Let’s explore each one step by step.
🧪 1. The .test() Method — Quick Pattern Checking
The .test() method is the simplest and fastest way to check whether a specific pattern exists within a string.
Syntax
regex.test(string)
Example
var re = /cat/;
console.log(re.test("The cat is here")); // true
console.log(re.test("No animal here")); // false
How It Works
- It checks whether the pattern (
/cat/) appears in the string. -
It returns a boolean:
true→ if a match is foundfalse→ if no match exists
When to Use
Use .test() when you only need to confirm the presence or absence of a pattern — not its position or details.
📦 2. The .exec() Method — Detailed Pattern Extraction
The .exec() method performs a deeper search. Instead of returning a simple true or false, it provides complete information about the match.
Syntax
regex.exec(string)
Example
var re = /c(at)/;
var result = re.exec("The cat is here");
console.log(result[0]); // "cat" → full match
console.log(result[1]); // "at" → captured group
console.log(result.index); // 4 → match position
How It Works
.exec()scans through the string for the first match.-
It returns an array containing:
result[0]→ full matched textresult[1],result[2], etc. → captured groups (if any)result.index→ starting position of the match
- If no match is found, it returns
null.
With the Global Flag (g)
When used with /g, .exec() can find multiple matches in a loop:
var re = /a/g;
var result;
while ((result = re.exec("banana")) !== null) {
console.log("Found:", result[0], "at index", result.index);
}
This method is ideal for extracting detailed match data or iterating through multiple occurrences in a string.
🔁 3. The .match() Method — Convenient String-Based Matching
While .test() and .exec() belong to the RegEx object, .match() is a String method that allows the same kind of pattern searching but from the string side.
Syntax
string.match(regex)
Example
var text = "The cat is here";
var result = text.match(/cat/);
console.log(result[0]); // "cat"
Global Search with /g
Using the global flag returns all matches in an array.
var str = "cat, bat, hat";
console.log(str.match(/at/g)); // ["at", "at", "at"]
Key Points
- Without
/g:.match()returns the first match as an array (like.exec()). - With
/g: it returns all matches as an array of strings, without detailed position info.
When to Use
Use .match() when you want a quick list of all matched items within a string, especially for display or summary purposes.
🧭 Choosing the Right Method
To summarize the differences clearly:
1. .test()
- Returns:
trueorfalse - Purpose: Check if a match exists
- Use Case: Quick validation checks
2. .exec()
- Returns: An array (or
null) - Purpose: Retrieve detailed match data
- Use Case: Extract information and positions from matches
3. .match()
- Returns: Array of matches (or
null) - Purpose: Find matches from the string perspective
- Use Case: Get all matching substrings in one go
Each method provides a different level of control — from simple checks to full pattern analysis.
💡 Practical Example
Let’s use all three methods on the same string:
var pattern = /cat/g;
var text = "The cat sat on another cat.";
console.log(pattern.test(text)); // true
console.log(pattern.exec(text)); // ["cat", index: 4]
console.log(text.match(pattern)); // ["cat", "cat"]
.test()confirms the word exists..exec()provides detailed match info..match()returns all matching occurrences.
🧩 Review – Fill in the Gaps
- 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.indextells you where the______started in the string. - All three —
.test(),.exec(), and.match()— are used to______for patterns inside strings.