JavaScript String match() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

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

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].

This page is part of JavaScript String Methods. Related topics include includes() and indexOf().

Understanding the match() Method

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.

📝 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:

SituationReturns
With g, matches foundArray of full match strings (no groups)
Without g, match foundArray: full match, then groups; plus index, input, optional groups
No matchnull
No argument[""]
Plain string patternConverted 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)];

⚡ Quick Reference

GoalCode
All matchesstr.match(/pat/g)
First match + groupsstr.match(/pat(group)/)
Miss?=== null
Boolean onlyregex.test(str)
All + groupsstr.matchAll(/pat/g)
Named groupfound.groups.name
Literal dot in string patternstr.match("1\\\\.3") or /1\.3/

🔍 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

📋 match() vs test() vs matchAll()

str.match(re)re.test(str)str.matchAll(re)
ResultArray or nulltrue / falseIterator of match arrays
Best forGet match text / groups (first)Simple yes/noAll matches with groups
Needs g?Optional (changes shape)NoYes (required)
MissnullfalseEmpty iterator

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"]
Try It Yourself

How It Works

With g, every complete match becomes one array element. Capturing groups are not included in this mode.

Example 2 — First Match and Capturing Groups

MDN demo: without g, groups and index are available.

JavaScript
const str = "For more information, see Chapter 3.4.5.1";
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);

found[0];     // "see Chapter 3.4.5.1"  (full match)
found[1];     // "Chapter 3.4.5.1"      (group 1)
found[2];     // ".1"                   (group 2)
found.index;  // 22
Try It Yourself

How It Works

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 AE in either case.

JavaScript
const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const regexp = /[a-e]/gi;
const matches = str.match(regexp);

console.log(matches);
// ["A", "B", "C", "D", "E", "a", "b", "c", "d", "e"]
Try It Yourself

How It Works

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" }
Try It Yourself

How It Works

Syntax (?<name>...) stores the capture under found.groups.name. Named groups need a non-global match (or matchAll) to appear with full group details.

Example 5 — No Argument, Misses, and String Patterns

Edge cases: empty match, null, and the dangerous . in strings.

JavaScript
"Nothing will come of nothing.".match();  // [""]

"hello".match(/xyz/);   // null

"123".match("1.3");     // ["123"]  (. means any character!)
"123".match("1\\.3");   // null     (escaped literal dot)
"123".match(/1\.3/);    // null
Try It Yourself

How It Works

A plain string is compiled as a regex. Prefer a regex literal when you need literal dots, or escape with \\. inside the string.

🚀 Common Use Cases

  • Extract patterns — emails, IDs, chapter numbers, codes.
  • 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.

📝 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.

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.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

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.”

Continue with includes(), indexOf(), or the String methods hub.

💡 Best Practices

✅ Do

  • Check for null before using the result
  • Use g when you want every full match as strings
  • Omit g (or use matchAll) when you need groups
  • Prefer test() for simple yes/no checks
  • Escape special characters in string patterns

❌ Don’t

  • Assume a miss returns []
  • Expect groups in a global match() result
  • Pass unescaped user text as a regex string
  • Use match when includes is enough for a fixed substring
  • Forget that . in a string pattern means “any character”

Key Takeaways

Knowledge Unlocked

Five things to remember about String.match()

Regex results as an array — or null when nothing matches.

5
Core concepts
🎯 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.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful