JavaScriptStrings

String.prototype.match()

Matches a string against a regular expression. Returns an array with the match or all matches, or null.

Returns
An array with the match and its groups (without the g flag), an array of all the matches (with the g flag), or null when nothing matches.
Changes the original
No
On this page

Example

#
JavaScript
const message = 'Task 12 for 30 pts, task 13 for 45 pts'

console.log(message.match(/\d+/g))
console.log(message.match(/(\d+) pts/))
console.log(message.match(/bonus/))
Console
['12', '30', '13', '45']
['30 pts', '30']
null

Definition and usage

#

The match() method checks a string against a regular expression and returns the pieces it found. When nothing matches, the result is null, so always check whether a match exists before using the result.

The result depends on the g flag. Without it, you get the details of the first match: index 0 holds the whole matched text, the following indexes hold the capture groups, and the index and groups properties hold its position and named groups. With the g flag, you get a plain array of all the matches, without groups.

When you need all the matches and their groups at the same time, use matchAll(), which returns an iterator. If you only want to check whether the pattern matches, regex.test(text) is simpler, returning true or false.

Syntax

#
Syntax
string.match(regexp)

Parameters

#
  • regexp

    RegExp | string

    A regular expression. A string gets converted to one via new RegExp(), so special characters, such as a dot, keep their special meaning.

More examples

#
Named groups and the match position
JavaScript
const text = 'Due: 2026-10-15'
const result = text.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/)

console.log(result.groups)
console.log(result.index)
Console
{ year: '2026', month: '10', day: '15' }
5
No match and counting safely
JavaScript
const text = 'No numbers in this sentence'
const numbers = text.match(/\d+/g) ?? []
console.log(numbers.length)

const hashtags = 'Learning #javascript and #css'.match(/#\w+/g)
console.log(hashtags)
Console
0
['#javascript', '#css']
Checking a format with test()
JavaScript
const codePattern = /^[A-Z]{3}-\d{4}$/

console.log(codePattern.test('ABC-2026'))
console.log(codePattern.test('abc-2026'))
console.log('ABC-2026'.match(codePattern)[0])
Console
true
false
ABC-2026

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • The result can be null, and then result[0] throws an error. Check for a match before using the result, or with the g flag write text.match(re) ?? [].
  • An expression with the g flag remembers its position in lastIndex, so calling test() repeatedly on the same object can produce alternating results. For a simple check, use an expression without g.
  • You can get all the matches together with their groups via [...text.matchAll(/(\d+) pts/g)].

Related entries

#

Spotted a mistake or missing an example? Write to us.