The Wayback Machine - https://web.archive.org/web/20240531182256/https://www.geeksforgeeks.org/javascript-string-match-method/
Open In App

JavaScript String match() Method

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript String match() method searches a string for a specified pattern using regular expressions. It returns an array of all matches found, or null if no matches are found.

String match() Method Syntax

string.match(regExp);

String match() Method Parameters

  • string: The string to be searched.
  • regexp: A regular expression object or a regular expression pattern string to be searched for within the string.

Note: If the g flag is not used with the regular expression, only the first match will be returned. If the g flag is used, all matches will be returned.

String match() Method Return Value

Returns an array that contains the matches and one item for each match or if the match is not found then it will return Null. 

String match() Method Examples

Example 1: Using /g flag

Here, the substring “eek” will match with the given string, and when a match is found, it will return an array of string objects. Here “g” flag indicates that the regular expression should be tested against all possible matches in a string. 

javascript




// Initializing function to demonstrate match()
// method with "g" para
function matchString() {
    let string = "Welcome to geeks for geeks";
    let result = string.match(/eek/g);
    console.log("Output : " + result);
} matchString();


Output

Output : eek,eek



Example 2: Using /i flag 

Here, the substring “eek” will match with the given string, and it will return instantly if it found the match. Here “i” parameter helps to find the case-insensitive match in the given string. 

javascript




// Initializing function to demonstrate match()
// method with "i" para
function matchString() {
    let string = "Welcome to GEEKS for geeks!";
    let result = string.match(/eek/i);
    console.log("Output : " + result);
} matchString();


Output

Output : EEK



Example 3: Using /gi flag

Here, the substring “eek” will match with the given string, and it will return instantly if it found the match. Here “gi” parameter helps to find the case-insensitive match AND all possible combinations in the given string. 

javascript




// Initializing function to demonstrate match()
// method with "gi" para
function matchString() {
    let string = "Welcome to GEEKS for geeks!";
    let result = string.match(/eek/gi);
    console.log("Output : " + result);
} matchString();


Output

Output : EEK,eek



We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browser



Previous Article
Next Article

Similar Reads

How to match multiple parts of a string using RegExp in JavaScript ?
In this article, we will try to understand how we could match multiple parts of a string using Regular Expression (RegExp) in JavaScript with the help of both theoretical explanations as well as coding examples too. Let us first understand some below enlightened theoretical details about Regular Expressions in JavaScript and the actual object (RegE
2 min read
What is match() Method in JavaScript ?
JavaScript String match() method is an inbuilt function in JavaScript used to search a string for a match against any regular expression. It returns an array containing the matches found, or null if no matches are found. Syntax: string.match(regExp);Parameters:regExp: (i.e. regular expression) which will compare with the given string. Example: Here
1 min read
TypeScript String match() method
TypeScript has a built-in match() function which is used to find a match, within a string against any given regular expression. If the match is discovered it will return it as an array. Syntax:string.match(regexp: string | RegExp): RegExpMatchArray | nullParameters:regexp: Pass RegExp to search for a string. Return Value If the match is found, then
1 min read
JavaScript Symbol match Property
JavaScript Symbol match property is used to identify the matching of a regular expression against a string and this function is called using String match() method. Syntax: regexp[Symbol.match] = false; Parameters: It does not accept any parameters. Return value: It will return the Boolean value for a string matching if matches found then it will re
1 min read
JavaScript Program to Find all Strings that Match Specific Pattern in a Dictionary
Finding all strings in a dictionary that match a specific pattern in JavaScript involves searching for words that follow a particular structure or format within a given dictionary. The pattern can include placeholders for characters, allowing flexibility in the search. In this article, we will explore various approaches for finding all strings that
5 min read
JavaScript Program to Match Single Character with Multiple Possibilities
In this article, we will write programs to check all possible matching in JavaScript. We will check the occurrence of characters individually in the given string or sequence. In this article, we will check the occurrence of vowels in the given string. Table of Content Using JavaScript RegExp test() MethodUsing JavaScript String match() MethodUsing
3 min read
HTTP headers | If-None-Match
The HTTP Header If-None-Match is a request-type header. Generally, it is used to update the entity tags on the server. Firstly, the Client provides the Server with a set of entity tags (E-tags). The Server compares the given tags with those it already has for the resource. Then, the Server will provide the requested page with a 200 status code only
2 min read
HTTP headers | If-Match
The HTTP headers If-Match is request-type header. It is used to make the request conditional. If it matches one of the listed conditional ETags then the server will send back the requested resource for PUT and other non-safe methods, it will only upload the resource in this case. This ETag header uses a string comparison algorithm. Using this heade
2 min read
Node.js assert.match() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.match() function expects the string input to match the regular expression. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.match(string, regexp[, message]) Parameters: This function accepts the follow
2 min read
How to make jQuery throw an error when it doesn't match an element ?
In this article, we will learn how to make a jQuery throw error when it does not match an element. Approach: To do this task we create a function and check if the given element exists or not, using the length property of the element. If an element exists the length of the element is greater than or equal to 1 otherwise the length of the element is
2 min read