RegExp.prototype.exec(string) runs a search and returns match details (full match, groups, position) or null if nothing matches. It is the detailed cousin of test().
Remember
regex.exec(string)
match found → array:
[0] full match
[1…] capture groups
.index start position
.input original string
no match → null
with /g: each call advances lastIndex
Create a regex, call exec, then read the result or handle null.
Create a RegExp (literal or new RegExp).
Call regex.exec(string).
If the result is not null, read [0], groups, and .index.
With the g flag, call exec again in a loop until it returns null.
Piece
Required?
Role
RegExp
Yes
Pattern to search with
string
Yes
Text to search in
Null check
Yes in practice
Avoid reading properties on null
Pseudocode
Pseudocode
regex = /pattern/flags
str = text to search
result = regex.exec(str)
if result is null:
// no match
else:
full = result[0]
group1 = result[1] // if any
pos = result.index
// with /g flag:
while (result = regex.exec(str)) is not null:
use result
// lastIndex advances each time
Reference
Cheat Sheet
Keep these facts handy while reading match results.
Topic
Detail
Syntax
regex.exec(string)
Success
Array with match + optional groups
Failure
null
result.index
Where the match starts
result.input
Original string
Global /g
Uses / updates lastIndex
vs test()
test → boolean; exec → details
Try it
Live Preview
Pick a pattern and see what exec() returns on sample text.
Browser demo of RegExp.prototype.exec — same idea as the examples below.
Result
regex = /cat/i
str = "The Cat sat"
result = regex.exec(str)
→ ["Cat"]
index: 4
input: "The Cat sat"
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Use the g flag./\d+/g can find more than one number.
Loop exec. Each call continues from lastIndex.
Print each hit. Match text and start index are logged.
Stop on null. When no more matches remain, exec returns null.
Edge Cases & Pitfalls
Check these before relying on exec() in real projects.
null
Always check for null
Reading result[0] when exec returned null throws. Guard with if (result).
lastIndex
Global regex remembers position
With /g, reuse can continue from a stale lastIndex. Reset with regex.lastIndex = 0 when restarting a search.
trap
Zero-length matches can loop forever
Patterns that can match empty strings (e.g. /a*/g on some inputs) need care — advance lastIndex manually if a match has length 0.
vs test
test() also updates lastIndex when global
Do not mix test and exec on the same global regex without resetting lastIndex.
Compatibility
Browser & Runtime Support
RegExp.prototype.exec() is a long-standing core method — available in every JavaScript environment.
✓ Baseline · Widely available
RegExp.prototype.exec()
Supported in all browsers and Node.js versions. Safe for every target with no polyfill.
100%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
exec()Universal
Bottom line: Use freely for match details and capture groups. Prefer test() when you only need a boolean.
Analysis
Performance Notes
Approach
Cost
Best for
regex.exec(str)
One search step
Details + groups
regex.test(str)
One search step
Boolean only (lighter intent)
Global exec loop
One step per match
All matches with groups
Prefer test() when you do not need groups or position. For many matches with captures, a global exec loop is the classic approach.
Remember
Key Takeaways
Role: return match details (or null), including groups and index.
Safety: always check for null before reading the result.
Global: loop exec() with /g until null; watch lastIndex.
vs test: use test() for yes/no; exec() for details.
One line:exec() gives you the match, groups, and position — or null when nothing matches.
Frequently Asked Questions
A result array when a match is found, or null when there is no match. Index 0 is the full match; indexes 1, 2, … hold capture groups. The result also has index (start position) and input (original string).
test() returns only true or false. exec() returns detailed match data — full text, groups, and position — so you can process captures.
On a global regex, each exec() call continues from lastIndex. A while loop collects every match until exec() returns null.
It returns null. Always check for null before reading result[0] or capture groups.
No. exec() is read-only on the input string. On a global or sticky regex it updates lastIndex on the RegExp object.
Use exec() when you need the matched text, capture groups, or the match position. Use test() for a simple yes/no check.