JavaScript match() method
JavaScript match() Method
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo">单击按钮显示matches</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/g); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Definition and usage
The match() method can retrieve the specified value within the string, or find an or Matching of multiple regular expressions.
If you want to know more regular expression tutorials, please check out this site: RegExp Tutorial and our RegExp Object Reference Manual.
Note: The match() method will retrieve a String Object to find one or more text that matches the regexp. The behavior of this method depends heavily on whether regexp has flag g. If the regexp does not have the g flag, then the match() method can only perform a match on the stringObject. If no matching text is found, match() returns null. Otherwise, it returns an array with information about the matching text it found.
Browser support
All major browsers support the match() method
Syntax
Parameter value
Parameter | Description |
---|---|
regexp | Required. A RegExp object that specifies the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor to convert it to a RegExp object. |
Return value
Type | Description |
---|---|
Array | Array to store matching results. The contents of this array depend on whether regexp has the global flag g. If no matching result is found, null is returned. |
Technical Details
JavaScript Version: | 1.2 |
---|
More examples
Instances
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo">单击按钮显示matches。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str = "The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/gi); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
Running examples»
Click the "Run Instance" button to view the online instance