String.prototype.match() retrieves the result of matching a string against a regular expression (same API as MDN String.prototype.match()). Learn global vs non-global results, capturing groups, named groups, null when missing, string-to-RegExp coercion, when to prefer test() / matchAll(), five examples, and try-it labs.
01
Kind
Instance method
02
Returns
Array or null
03
With g
All full matches
04
Without g
First + groups
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
When you need pattern matching — emails, chapter numbers, capital letters, or pieces of a sentence — regular expressions are the tool, and match() is the string method that runs them.
The result depends on the global (g) flag. With g, you get every full match as a simple array. Without g, you get the first match plus capturing groups, index, and input — similar to RegExp.prototype.exec().
💡
Beginner tip
No match returns null, not an empty array. Always check if (found) before reading found[0].
Under the hood, str.match(regexp) mostly calls the argument’s Symbol.match method (for a normal regex, that is RegExp.prototype[Symbol.match]).
Pass a RegExp (or anything with Symbol.match).
Non-regex values are turned into new RegExp(value).
With g — all full matches; capturing groups are omitted.
Without g — first match + groups + index / input.
Miss — returns null.
Foundation
📝 Syntax
General form of String.prototype.match:
JavaScript
str.match(regexp)
Parameters
regexp — a RegExp, or any object with a Symbol.match method. Other values become new RegExp(regexp). Calling match() with no argument is like matching /(?:)/ and returns [""].
Return value
An Array (shape depends on g), or null:
Situation
Returns
With g, matches found
Array of full match strings (no groups)
Without g, match found
Array: full match, then groups; plus index, input, optional groups
No match
null
No argument
[""]
Plain string pattern
Converted via new RegExp(string) (remember to escape ., etc.)
Common patterns
JavaScript
"The quick brown fox".match(/[A-Z]/g); // ["T"]
const found = "see Chapter 3".match(/see (chapter \d+)/i);
found[0]; // "see Chapter 3"
found[1]; // "Chapter 3"
found.index; // start index of the full match
// Yes/no only → prefer test:
/fox/.test("fox jumps"); // true
// All matches + groups → prefer matchAll:
[..."a1b2".matchAll(/(\d)/g)];
Cheat Sheet
⚡ Quick Reference
Goal
Code
All matches
str.match(/pat/g)
First match + groups
str.match(/pat(group)/)
Miss?
=== null
Boolean only
regex.test(str)
All + groups
str.matchAll(/pat/g)
Named group
found.groups.name
Literal dot in string pattern
str.match("1\\\\.3") or /1\.3/
Snapshot
🔍 At a Glance
Four facts to remember about String.match().
Returns
Array | null
Never empty array on miss
Flag g
all matches
No capturing groups
No g
first + groups
Like exec()
Mutates
no
Strings stay immutable
Compare
📋 match() vs test() vs matchAll()
str.match(re)
re.test(str)
str.matchAll(re)
Result
Array or null
true / false
Iterator of match arrays
Best for
Get match text / groups (first)
Simple yes/no
All matches with groups
Needs g?
Optional (changes shape)
No
Yes (required)
Miss
null
false
Empty iterator
Hands-On
Examples Gallery
Examples follow MDN String.match() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Find matches with and without capturing groups.
Example 1 — Basic Global match()
MDN-style: find every uppercase letter with the g flag.
JavaScript
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
console.log(found);
// ["T", "I"]
Parentheses create capturing groups. Index 0 is always the full match; later indexes are the groups in order. The i flag makes the search case-insensitive.
📈 Practical Patterns
Flags, named groups, and string-to-RegExp pitfalls.
Example 3 — Global + Ignore Case
MDN demo: collect every letter from A–E in either case.
g finds every match; i ignores case so both A and a qualify.
Example 4 — Named Capturing Groups
MDN-style: read a group by name from found.groups.
JavaScript
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);
console.log(found.groups);
// { animal: "fox" }
List all matches — use the g flag for a simple string array.
Read capturing groups — omit g (or use matchAll for every match).
Named parts — (?<name>...) + found.groups.
Yes/no only — prefer regex.test(str).
Not for plain substring search — use includes() / indexOf() when you do not need a pattern.
🧠 How match() Resolves a Pattern
1
Receive a pattern
Use a RegExp, or coerce other values with new RegExp(...).
Input
2
Run Symbol.match
The regex implementation searches the string for matches.
Search
3
Shape the result
With g: all full matches. Without g: first match + groups.
Result
4
✅
Return Array or null
Check for null before reading indexes or groups.
Important
📝 Notes
No match returns null — not [].
With g, capturing groups are omitted from the result.
For all matches and groups, use matchAll() (requires g).
String arguments become regexes — escape special characters when you need literals.
Calling match() with no argument returns [""].
Prefer test() when you only need a boolean.
Compatibility
Browser & Runtime Support
String.prototype.match() is Baseline Widely available — supported across browsers for many years. Named capturing groups are widely supported in modern engines.
✓ Baseline · Widely available
String.match()
Safe for production pattern extraction. Remember null on miss, and use matchAll when you need every match with groups.
UniversalWidely available
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
match()Excellent
Bottom line: Use String.match() to extract regex matches from a string. Check for null, know how the g flag changes the result, and prefer test() or matchAll() when they fit better.
Wrap Up
Conclusion
String.prototype.match() runs a regular expression against a string and returns an array of results — or null if nothing matches. The g flag switches between “all full matches” and “first match with groups.”
Use match when includes is enough for a fixed substring
Forget that . in a string pattern means “any character”
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.match()
Regex results as an array — or null when nothing matches.
5
Core concepts
📝01
Returns
Array / null
API
🎯02
Flag g
all full matches
Regex
📦03
No g
groups + index
Capture
❌04
Miss
null
Bounds
📄05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.match(regexp) returns the result of matching the string against a regular expression: an Array of matches (shape depends on the g flag), or null if nothing matches.
With g, you get an array of all full matches (no capturing groups). Without g, you get the first match plus capturing groups, index, and input — similar to RegExp.prototype.exec().
When there is no match. Always check for null before reading array indexes.
With the g flag, match() does not include groups. Use String.prototype.matchAll() or loop with RegExp.prototype.exec() instead.
It is converted with new RegExp(string). Special characters like . mean “any character” unless you escape them (for example 1\.3).
No. Strings are immutable. match() only reads and returns an array or null.
Did you know?
Passing a number like 65 or even NaN works because match builds new RegExp(65) / new RegExp(NaN) — which search for the text "65" or "NaN". Handy to know; usually clearer to write an explicit regex.