Home » JavaScript String match() Method

JavaScript String match() Method

by Online Tutorials Library

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.

Test it Now

Output:

Java  

Example 2

In this example, we will search for a regular expression using global flag.

Test it Now

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.

Test it Now

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:

Test it Now

Output:

Java  

Example 5

Here, we will print the array of matched elements.

Test it Now

Output:

a,a,p,o,i,n  

Example 6

Let’s see the same example without using global search.

Test it Now

Output:

a  

Next TopicJavaScript String

You may also like