JavaScript String match() Method
The JavaScript string match() method is used to match the string against a regular expression. We can use global search modifier with match() method to get all the match elements otherwise the method return only first match.
Syntax
The match() method is represented by the following syntax:
Parameter
regexp – It represents the regular expression which is to be searched.
Return
The matched regular expression.
JavaScript String match() Method Example
Let’s see some simple examples of match() method.
Example 1
Let’s see a simple example to search for a match.
Output:
Java
Example 2
In this example, we will search for a regular expression using global flag.
Output:
Java
Example 3
Let’s see one more example to search for a regular expression using global flag. As match() method is case-sensitive, it return null in this case.
Output:
null
Example 4
We can ignore case-sensitive behaviour of match() method by using ignore flag. Let’s understand with the help of example:
Output:
Java
Example 5
Here, we will print the array of matched elements.
Output:
a,a,p,o,i,n
Example 6
Let’s see the same example without using global search.
Output:
a