JavaScript String match() Method | javascript match regex
ExampleSearchastringfor"ain":letstr="TheraininSPAINstaysmainlyintheplain";str.match(/ain/g) //Returnsain,ain,ainTryitYourself»[1]More"TryitYourself"examplesbelow.DefinitionandUsageThematch()methodsearchesastringforamatchagainstaregularexpression,andreturnsthematches,asanArrayobject.ReadmoreaboutregularexpressionsinourRegExpTutorial[2]andourRegExpObjectReference[3].Note:Iftheregularexpressiondoesnotincludethegmodifier(toperformaglobalsearch),thematch()methodwillreturnonlythefirstmatchinthes...
ExampleSearch a string for "ain":
let str = "The rain in SPAIN stays mainly in the plain"; str.match(/ain/g) // Returns ain,ain,ain
Try it Yourself »[1]More "Try it Yourself" examples below.
Definition and UsageThe match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
Read more about regular expressions in our RegExp Tutorial[2] and our RegExp Object Reference[3].
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.
Browser Supportmatch() is fully supported in all browsers:
Chrome IE Edge Firefox Safari Opera Yes Yes Yes Yes Yes Yes Syntax Parameter Values Parameter Description regexp Required. T...