Introduction
Capturing Groups are one of the most powerful features of Regular Expressions (Regex). They allow you to capture specific parts of a matched pattern so that the captured values can be accessed and used later in your program.
Capturing groups are created by enclosing part of a regular expression inside parentheses ().
In Node.js, capturing groups are commonly used to extract names, dates, phone numbers, product codes, IDs, URLs, and other structured information from text.
For automation engineers, capturing groups are extremely useful for extracting dynamic values from API responses, log files, URLs, HTML content, and application messages.
In this tutorial, you’ll learn how capturing groups work and how they are used in Node.js.
What are Capturing Groups?
A capturing group is a part of a regular expression enclosed in parentheses ().
It captures the matched portion of the text for later use.
Syntax
/(pattern)/
Example:
const regex =
/(Node)/;
Why Use Capturing Groups?
Capturing groups allow applications to:
Extract specific values
Separate text into parts
Parse structured data
Process API responses
Validate user input
Extract IDs and tokens
Process log files
Example 1: Basic Capturing Group
const regex =
/(Node)/;
const result =
regex.exec("Node.js");
console.log(result);
Sample Output
[
'Node',
'Node',
index: 0,
input: 'Node.js'
]
The first element contains the complete match, and the second element contains the captured group.
Example 2: Multiple Capturing Groups
const regex =
/(\w+)\s(\w+)/;
const result =
regex.exec("John Smith");
console.log(result);
Sample Output
[
'John Smith',
'John',
'Smith'
]
Complete match →
John SmithGroup 1 →
JohnGroup 2 →
Smith
Access Individual Groups
const regex =
/(\w+)\s(\w+)/;
const result =
regex.exec("Alice Johnson");
console.log(result[1]);
console.log(result[2]);
Sample Output
Alice
Johnson
Example 3: Capture Numbers
const regex =
/Order-(\d+)/;
const result =
regex.exec("Order-1256");
console.log(result[1]);
Sample Output
1256
Example 4: Capture Date Parts
const regex =
/(\d{2})-(\d{2})-(\d{4})/;
const result =
regex.exec("25-12-2025");
console.log(result[1]);
console.log(result[2]);
console.log(result[3]);
Sample Output
25
12
2025
Example 5: Capture Email Parts
const regex =
/(\w+)@(\w+\.\w+)/;
const result =
regex.exec("admin@example.com");
console.log(result[1]);
console.log(result[2]);
Sample Output
admin
example.com
Real-World Example
Extract a product code from text.
const text =
"Product: PRD1005";
const regex =
/Product:\s(PRD\d+)/;
const result =
regex.exec(text);
console.log(result[1]);
Sample Output
PRD1005
Automation Testing Example
Capturing groups are widely used in automation frameworks.
Playwright Example
Extract an order ID from page text.
const text =
"Order ID: ORD4567";
const regex =
/Order ID:\s(ORD\d+)/;
const result =
regex.exec(text);
console.log(result[1]);
Selenium Example
Extract a username from an email address.
const email =
"john@example.com";
const regex =
/(\w+)@(\w+\.\w+)/;
const result =
regex.exec(email);
console.log(result[1]);
Cypress Example
Extract a product ID.
const product =
"PID-7890";
const regex =
/PID-(\d+)/;
const result =
regex.exec(product);
console.log(result[1]);
API Testing Example
Extract a token from a response.
const response =
"Token: ABC123XYZ";
const regex =
/Token:\s(\w+)/;
const result =
regex.exec(response);
console.log(result[1]);
Data-Driven Testing Example
Extract employee IDs.
const employees = [
"EMP1001",
"EMP1002",
"EMP1003"
];
const regex =
/EMP(\d+)/;
employees.forEach(employee => {
const result =
regex.exec(employee);
console.log(result[1]);
});
Sample Output
1001
1002
1003
Common Mistakes
Forgetting Parentheses
Incorrect:
/\d+/
This matches digits but does not create a capturing group.
Correct:
/(\d+)/
Accessing the Wrong Group Index
const result =
/(\w+)-(\d+)/.exec("ORD-100");
result[0]→ Entire match (ORD-100)result[1]→ORDresult[2]→100
Forgetting to Check for a Match
Incorrect:
const result =
regex.exec(text);
console.log(result[1]);
If there is no match, result is null, causing an error.
Correct:
const result =
regex.exec(text);
if (result) {
console.log(result[1]);
}
Best Practices
Use parentheses only for values you need to capture.
Always check whether a match exists before accessing captured groups.
Keep regular expressions simple and readable.
Use meaningful variable names.
Test patterns with different input values.
Document complex regex patterns with comments.
Escape special characters when required.
Conclusion
Capturing groups make Regular Expressions much more powerful by allowing you to extract specific parts of matched text. Instead of simply checking whether a pattern exists, you can retrieve individual values and use them in your application.
For automation engineers, capturing groups are essential for extracting IDs, tokens, dates, product codes, usernames, and other dynamic values from API responses, logs, URLs, and application data.
Mastering capturing groups will help you build more flexible Node.js applications and more reliable automation frameworks.
Frequently Asked Questions (FAQs)
What is a capturing group?
A capturing group is a part of a regular expression enclosed in parentheses () that stores the matched text.
Which method is commonly used with capturing groups?
The exec() method, because it returns the complete match along with all captured groups.
What does result[0] contain?
The entire matched string.
What does result[1] contain?
The first captured group.
Why are capturing groups important in automation testing?
Automation engineers use capturing groups to extract IDs, tokens, usernames, product codes, dates, URLs, and other dynamic values from API responses, logs, HTML pages, and application messages.
Key Takeaways
Capturing groups are created using parentheses
().They allow specific parts of matched text to be extracted.
exec()returns both the full match and captured groups.result[0]contains the entire match.result[1],result[2], and so on contain the captured groups.Always check whether a match exists before accessing group values.
Capturing groups simplify text parsing and data extraction.
They are widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
Keep regex patterns readable and maintainable.
Mastering capturing groups is essential for backend development and automation testing.
