Tutorials Tonight's Logo
Follow Us

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" ]
▶ Try It

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)

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);
▶ Try It

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:

  1. 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);
    ▶ Try It
  2. 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" ]
    ▶ Try It
  3. Nothing matches:-
    If nothing matches in the string then it always returns null.

    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);
▶ Try It

# 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);
▶ Try It

# 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);
▶ Try It

# 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"]
▶ Try It
string match javascript

The additional properties are:

  1. index: It is the index at which match was found
  2. input: It is the search string method is applied upon.
  3. groups: It is capturing group whose keys and value are the names and capturing group respectively.


Points to remember:

  1. The match() string method finds values in the string using regular expression.
  2. It returns an array of matching element if no element matched then returns null.

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.