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
gflag), an array of all the matches (with thegflag), ornullwhen nothing matches. - Changes the original
- No
Example
#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/))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
#string.match(regexp)Parameters
#regexp
RegExp | string
A regular expression. A string gets converted to one vianew RegExp(), so special characters, such as a dot, keep their special meaning.
More examples
#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)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)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])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 thenresult[0]throws an error. Check for a match before using the result, or with thegflag writetext.match(re) ?? []. - An expression with the
gflag remembers its position inlastIndex, so callingtest()repeatedly on the same object can produce alternating results. For a simple check, use an expression withoutg. - You can get all the matches together with their groups via
[...text.matchAll(/(\d+) pts/g)].
Related entries
#- RegExpA regular expression: a pattern for searching, validating, and replacing pieces of text.
- String.prototype.replace()Returns a new string in which the first match of a pattern (or all matches, with the g flag) has been replaced with other text.
- String.prototype.split()Splits a string into an array of pieces based on a separator: a character, a string, or a regular expression.
- String.prototype.includes()Checks whether a string contains the given piece of text. Returns true or false, and is case-sensitive.
- String.prototype.indexOf()Returns the index of the first occurrence of the given text in a string, or -1 when it is not there.
- String.prototype.matchAll()Returns an iterator over all the matches of a regular expression, each one together with its groups and position in the text.
Spotted a mistake or missing an example? Write to us.