Trusted answers to developer questions

What is the match() method JavaScript?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The string.match() is a built-in function in JavaScript; it is used to search a string, for a match, against a regular expression. If it returns an Array object the match is found, if no match is found a Null value is returned.


Syntax

The syntax for string.match() is shown below:

svg viewer

Regular expression

It has two things

  • an expression that needs to be searched or replaced

  • a modifier that modifies the search

svg viewer
modifiers meaning
g It searches the expression at all instances.
i It performs case-insensitive matching.

Examples

  1. This example shows a regular expression with a ‘g’ modifier
var data = 'Welcome to the Educative! Everyone likes Educative';
// regular expression with 'g' modifier
var regExp = /Educative/g;
// string.match() function.
var found = data.match(regExp);
// printing an array object.
console.log(found);
  1. This example shows a regular expression with a ‘i’ modifier
var data = 'Welcome to the Educative! Everyone likes Educative';
// regular expression with 'i' modifier
var regExp = /educative/i;
// string.match() function.
var found = data.match(regExp);
// printing an array object.
console.log(found);
  1. This example shows a regular expression with both a ‘g’ and an ‘i’ modifier
var data = 'Welcome to the Educative! Everyone likes Educative';
// regular expression with 'g' 'i' both modifier
var regExp = /educative/gi;
// string.match() function.
var found = data.match(regExp);
// printing an array object.
console.log(found);

RELATED TAGS

match method
javascript
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?