What is the string match() method in JavaScript?

The method match() takes a regular expression (regex) and matches it against a provided string.

Syntax

string.match(reg_exp)

Parameters

reg_exp: The regular expression that is matched against the string.

Return value

The match() method returns an array containing the matches from the string.

Code

Here is a coded example of the match() method.

// lets look for all instances of 'ea' and 'in' in the following sentence.
let str = "What a beautiful weather in Berlin.";
console.log(str.match(/ea/g))
console.log(str.match(/in/g))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved