a shot of dev knowledge

Related tags

What is the match() method JavaScript?

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

RELATED COURSES
View all Courses
Related Courses
Related Courses
View all Courses

Keep Exploring