13
Oct
Learn how to use JavaScript match() Method
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
Note: If the regular expression does not include the g modifier (to perform a global search), the match() method will return only the first match in the string.
This method returns null if no match is found.
The match() method is supported in all major browsers.
Read more about Regular Expressions
Syntax
string.match(regexp)
Parameter Values
regexp : Required. The value to search for, as a regular expression.
Return Value
Array : An array containing the matches, one item for each match, or null if no match is found
JavaScript Version: 1.2
Example
var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/gi); The result of n will be: ain,AIN,ain,ain