JavaScript String match() Method: Get All The Matchings From A String
In this tutorial, you will learn about the match() method in JavaScipt and will know how to match certain characters or substring within a string using regular expression.
JavaScript String match
The match() method in JavaScript matches a series characters within a string using a regular expression.
The method accepts a regular expression as an argument and use that regular expression to find any match. If the match is found then it returns the matching content from the string as an array of strings.
Example
const str = "Learning To Code Is Learning To Create And Innovate";
const exp = /[A-Z]/g; // Matching capital characters
const matches = str.match(exp);
console.log(matches);
// output: [ "L", "T", "C", "I", "L", "T", "C", "A", "I" ]
The above example use /[A-Z]/g
as a regular expression which matches all the capital letters from the string and returns as an array.
Syntax Of match() method
The syntax of the string match() method is following:
string.match(regexp)
- string: It is the string you want to match for.
- regexp - It is the given regular expression to match within the string. If the given regexp is not a regular expression then the method implicitly converts it to regex by using new RegExp(regexp).
Example: non-regexp
const str = "learning to code is learning to create and innovate";
const regexp = "to"; // not a regular expression
const matches = str.match(regexp);
console.log(matches);
In the above example, the passed argument in the match method is not a regex so the method implicitly converted it to regex and matched the content.
If regexp is not passed then it returns an array with 1 empty string. i.e ['']
Example: no regexp
const str = "learning to code is learning to create and innovate";
const matches = str.match(); // no regexp passed
console.log(matches);
Return from match() method
The method returns an array of matching content that depends on the presence and absence of global flag (g) in the given regexp and matching contents. Which creates 3 different modes:
-
regex doesn't have g flag:-
If the g flag is not used in regex then the method returns only the first complete match and its capturing group with the index (position of the match) and input (input string).Example: global flag (g)
const str = "learning to code is learning to create and innovate"; // without g flag const regexp = /l(earn)ing/; const matches = str.match(regexp); console.log(matches); // [ "learning", "earn" ] // Additional information console.log(matches.index); console.log(matches.input);
-
regex have g flag:-
If the g flag is used in the regex then it returns all the matching within the string without capturing groups and other details, that's why we need the matchAll method.Example: global flag (g)
const str = "learning to code is learning to create and innovate"; // with g flag const regexp = /l(earn)ing/g; const matches = str.match(regexp); console.log(matches); // [ "learning", "learning" ]
-
Nothing matches:-
If nothing matches in the string then it always returnsnull
.Example: Nothing matches
const str = "learning to code is learning to create and innovate"; const regexp = /[0-9]/g; // matching numbers const matches = str.match(regexp); console.log(matches); // null
Case sensitivity
One thing to note about regex is that it is case sensitive.
For example, if you want to match 'to' in an example then it will find for only 'to' not 'To', 'tO' or 'TO'.
Example: case sensitive
const str = "To do or not to do.";
const regexp = /to/g;
console.log(str.match(regexp));
If you want to get all occurrences of characters or substring regardless of their case then use flag i with regular expression.
Example: using flag i
const str = "To do or not to do.";
const regexp = /to/gi;
console.log(str.match(regexp));
Using match String In JavaScript
# Example 1: Matching all integers
In the following example use the match() function to get all the integers from 0-9.
Match all integers
const str = "Adding 15 an 25 gives 40.";
const regexp = /[0-9]/g; // matching numbers
const matches = str.match(regexp);
console.log(matches);
# Example 2: Ignoring cases
In the following example find the character from the string by ignoring its cases.
Use flag i to ignore cases in regular expression (both upper and lower case selection).
Ignore cases
const str = "ABWTCDXGOHI12355abcvfgthi";
// matching all alphabets
const regexp = /[A-Z]/gi;
const matches = str.match(regexp);
console.log(matches);
# Example 3: Matching a pattern
The following example finds all 'example' followed by 1 or more numeric character followed by decimals. Like 'example 1.1.1' to 'Example 2.5'
Match all 'example'
const str = "Jump from Example 2.3.5 to example 5.5.1";
// matching all examples
const regexp = /(example) \d[(.)\d]*/gi;
const matches = str.match(regexp);
console.log(matches);
# Example 4: Random search
The following example contains random non-RegExp objects as a parameter to the match() method.
const str = "A random line with Infinity to Infinity and 100, NaN and null too.";
console.log(str.match("random")); // ["random"]
console.log(str.match(NaN)); // ["NaN"]
console.log(str.match(Infinity)); // ["Infinity"]
console.log(str.match(+Infinity)); // ["Infinity"]
console.log(str.match(100)); // ["100"]
console.log(str.match(+100)); // ["100"]
console.log(str.match(null)); // ["null"]

The additional properties are:
- index: It is the index at which match was found
- input: It is the search string method is applied upon.
- groups: It is capturing group whose keys and value are the names and capturing group respectively.
Points to remember:
- The match() string method finds values in the string using regular expression.
- It returns an array of matching element if no element matched then returns
null
.