Introduction
Pattern matching is the process of searching text using a Regular Expression (Regex) to determine whether a specific sequence of characters exists. Instead of comparing entire strings, regex allows you to search for flexible patterns such as numbers, words, email addresses, dates, phone numbers, and more.
In Node.js, pattern matching is commonly performed using methods like test(), match(), exec(), and search().
For automation engineers, pattern matching is extremely useful for validating API responses, checking form inputs, extracting values from logs, verifying dynamic data, and testing application output.
In this tutorial, you’ll learn how pattern matching works in Node.js using Regular Expressions.
What is Pattern Matching?
Pattern matching is the process of checking whether text matches a particular regular expression.
Example:
const regex = /Node/;
console.log(
regex.test("Node.js Tutorial")
);
Sample Output
true
Why Use Pattern Matching?
Pattern matching helps applications:
-
Search text
-
Validate user input
-
Find specific words
-
Extract information
-
Verify API responses
-
Process log files
-
Perform automation testing
Method 1: Using test()
The test() method checks whether a pattern exists in a string.
const regex = /JavaScript/;
const result =
regex.test("Learning JavaScript");
console.log(result);
Sample Output
true
Method 2: Using match()
The match() method returns the matched text.
const text =
"Node.js is powerful";
const result =
text.match(/Node\.js/);
console.log(result);
Sample Output
[
'Node.js',
index: 0,
input: 'Node.js is powerful'
]
Method 3: Using search()
The search() method returns the index of the first match.
const text =
"Learn JavaScript";
console.log(
text.search(/JavaScript/)
);
Sample Output
6
Method 4: Using exec()
The exec() method returns detailed information about the match.
const regex = /Node/;
const result =
regex.exec("Node.js");
console.log(result);
Sample Output
[
'Node',
index: 0,
input: 'Node.js'
]
Match Digits
const regex = /\d+/;
const result =
"Order123".match(regex);
console.log(result);
Sample Output
[ '123' ]
Match Letters
const regex = /[A-Za-z]+/;
console.log(
regex.test("NodeJS")
);
Sample Output
true
Match Multiple Occurrences
Use the g (global) flag.
const text =
"cat dog cat bird cat";
const matches =
text.match(/cat/g);
console.log(matches);
Sample Output
[ 'cat', 'cat', 'cat' ]
Case-Insensitive Matching
Use the i flag.
const regex =
/node/i;
console.log(
regex.test("Node")
);
Sample Output
true
Real-World Example
Check whether a product code follows the required format.
const productCode =
"PRD1001";
const regex =
/^PRD\d+$/;
console.log(
regex.test(productCode)
);
Sample Output
true
Automation Testing Example
Pattern matching is widely used in automation frameworks.
Playwright Example
Validate the page title.
const title =
"Node.js Tutorial";
const regex =
/Node\.js/;
console.log(
regex.test(title)
);
Selenium Example
Validate an email address.
const email =
"admin@example.com";
const regex =
/^[^\s@]+@[^\s@]+\.[^\s@]+$/;
console.log(
regex.test(email)
);
Cypress Example
Verify an order number.
const orderId =
"ORD5678";
const regex =
/^ORD\d+$/;
console.log(
regex.test(orderId)
);
API Testing Example
Validate a response status.
const response = {
status: "SUCCESS"
};
const regex =
/^SUCCESS$/;
console.log(
regex.test(response.status)
);
Data-Driven Testing Example
Validate usernames from test data.
const users = [
"john123",
"alice456",
"bob789"
];
const regex =
/^[a-z]+\d+$/;
users.forEach(user => {
console.log(
regex.test(user)
);
});
Sample Output
true
true
true
Common Mistakes
Forgetting the Global Flag
Incorrect:
const text =
"cat cat cat";
console.log(
text.match(/cat/)
);
Only the first occurrence is returned.
Correct:
console.log(
text.match(/cat/g)
);
Ignoring Case Sensitivity
Incorrect:
/node/
Searching:
Node
Returns:
false
Use:
/node/i
Forgetting to Escape Special Characters
Incorrect:
/Node.js/
Correct:
/Node\.js/
Best Practices
-
Use
test()for simple true/false checks. -
Use
match()when you need the matched text. -
Use
exec()for detailed match information. -
Use
search()to find the position of a match. -
Escape special characters when matching them literally.
-
Use the
gflag for multiple matches. -
Keep regex patterns readable and well documented.
Conclusion
Pattern matching is one of the most practical applications of Regular Expressions in Node.js. It allows developers to efficiently search, validate, and extract information from text.
For automation engineers, pattern matching is essential for validating user inputs, checking API responses, verifying dynamic content, processing logs, and building reliable automated tests.
Mastering pattern matching will help you write cleaner, more efficient Node.js applications and automation frameworks.
Frequently Asked Questions (FAQs)
What is pattern matching?
Pattern matching is the process of searching text using a regular expression.
Which method returns true or false?
The test() method.
Which method returns the matched text?
The match() method.
Which method returns the position of a match?
The search() method.
Why is pattern matching important in automation testing?
Automation engineers use pattern matching to validate emails, phone numbers, IDs, API responses, URLs, product codes, log files, and dynamic application data.
Key Takeaways
-
Pattern matching searches text using Regular Expressions.
-
Use
test()for boolean checks. -
Use
match()to retrieve matched text. -
Use
search()to find the index of a match. -
Use
exec()for detailed match information. -
The
gflag matches all occurrences. -
The
iflag enables case-insensitive matching. -
Escape special characters when matching them literally.
-
Pattern matching is widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
-
Mastering pattern matching is essential for backend development and automation testing.
