JavaScript String match() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.match(regexp) matches a string against a regular expression and returns an array of results, or null if nothing matches. The array shape depends on the g flag. Need every match with groups? Prefer matchAll().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.match. Start with this line.

JavaScript
str.match(regexp)
CallWhat it does
str.match(regexp)Primary signature.
string.match(...)Call with the arguments your case needs.

Built into JavaScript. Call match(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.match(regexp)

Parameters

ParameterDescription
regexpRequired. Pattern; strings are coerced to RegExp
g flagOptional. All full matches vs first match + groups

Return Value

Return ValueDescription
*Array or null

JavaScript Examples

Example 1 — Global match()

Find every uppercase letter with the g flag.

JavaScript
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const found = paragraph.match(/[A-Z]/g);

console.log(found);
// ["T", "I"]
Try It Yourself

How It Works

  1. Build the pattern. /[A-Z]/g means “one uppercase letter, find all”.
  2. Scan the string. match collects every full match.
  3. Get a simple array. Result is ["T", "I"] — no groups in global mode.
  4. No match would be null. Always guard before using the array.

Example 2 — First Match and Capturing Groups

Without g, groups and index are available.

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

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

How It Works

  1. No g flag. Only the first match is returned, with group details.
  2. Index 0 is the full match. Later indexes are capturing groups.
  3. Nested groups. The inner (\.\d)* provides group 2 (".1").
  4. Case-insensitive. The i flag lets Chapter match chapter.

Example 3 — Global + Ignore Case

Collect every letter from A–E in either case.

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

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

How It Works

  1. g finds every match. The scan continues after each hit.
  2. i ignores case. Both A and a qualify.
  3. Character class. [a-e] is the range of letters to keep.
  4. Result is flat. Global mode returns full matches only — use matchAll for groups on every hit.

Browser & Runtime Support

String.prototype.match() is a long-standing language feature. It works in every modern browser, Internet Explorer, and every current Node.js / Deno / Bun release.

✓ Baseline · Widely available

String.prototype.match()

Supported in all evergreen browsers, Internet Explorer (legacy), and every current JavaScript runtime. Named capturing groups need modern engines (ES2018+).

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge Supported · Chromium & Legacy
Full support
Internet Explorer IE 4+ · Legacy (no named groups)
Full support
Opera All 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
String.match() Universal

Bottom line: Safe everywhere for basic matching. Prefer matchAll() when you need every match with groups. Named groups require a modern runtime.

Frequently Asked Questions

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.

Next: matchAll()

Iterate every regex match together with its capturing groups.

matchAll() tutorial →

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.

11 people found this page helpful