What is match() Method in JavaScript ?
Last Updated :
12 Feb, 2024
JavaScript String match() method is an inbuilt function in JavaScript used to search a string for a match against any regular expression. It returns an array containing the matches found, or null
if no matches are found.
Syntax:
string.match(regExp);
Parameters:
- regExp: (i.e. regular expression) which will compare with the given string.
Example: Here, the match()
method is called on the str
string with the regular expression /fox/g
as its argument. This regular expression searches for the word “fox” within the string. Since “fox” is present in the string, the method returns an array ["fox"]
, which contains the match found.
Javascript
const str = "The quick brown fox jumps over the lazy dog" ;
const matches = str.match(/fox/g);
console.log(matches);
|
Share your thoughts in the comments
Please Login to comment...