Home >>JavaScript String Methods >JavaScript String match() Method
JavaScript match() method is used to search a string for a match against a given regular expression and return the matches as an Array object. It returns null as output if no match is found.
Syntax:
string.match(regexp)
Parameter | Description |
---|---|
regexp | This is a required parameter. It defines the value to search for as a regular expression. |
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
match() | YES | YES | YES | YES | YES |
Here is an example of match() method:
<html> <body> <p>Click the button to see the Output.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var str = "php PHP PHPTPOINT Php Project"; var res = str.match(/PHP/g); document.getElementById("demo").innerHTML = res; } </script> </body> </html>
Click the button to see the Output.
Example 2:
<html> <body> <p>Click the button to see the Output.</p> <button onclick="myFunction1()">Try it</button> <p id="demo1"></p> <script> function myFunction1() { var str = "php PHP PHPTPOINT Php Project"; var res = str.match(/PHP/gi); document.getElementById("demo1").innerHTML = res; } </script> </body> </html>
Click the button to see the Output.