What is match() Method in JavaScript ?
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); // Output: ["fox"] |
Output
[ 'fox' ]
Contact Us