JavaScript match() method


The match() method in JavaScript is used to search a string for a match against a regular expression (regexp). It returns an array containing the results of the match, or null if no match is found. This method is useful for extracting information from a string based on specific patterns defined by regular expressions.

Syntax:

string.match(regexp)
  • regexp: A regular expression object or a string that represents the regular expression to search for in the string.

Return Value:

  • Returns an array containing the matched results. If there are no matches, it returns null.
  • If the regular expression has the global flag (g), the return value is an array of all matches found in the string.
  • If the global flag is not used, the return value is an array with the first match and additional properties, such as index (the index of the match) and input (the original string).

Example 1: Basic Usage

let text = "The quick brown fox jumps over the lazy dog."; let result = text.match(/quick/); console.log(result); // [ 'quick', index: 4, input: 'The quick brown fox jumps over the lazy dog.', groups: undefined ]

In this example, the match() method finds the substring "quick" in the text and returns an array containing the match.

Example 2: No Match Found

If the regular expression does not match anything in the string, match() will return null.

let text = "Hello, world!"; let result = text.match(/goodbye/); console.log(result); // null

Example 3: Using the Global Flag (g)

If the regular expression includes the global flag (g), the method returns an array of all matches found in the string.

let text = "The rain in Spain stays mainly in the plain."; let result = text.match(/in/g); console.log(result); // [ 'in', 'in', 'in' ]

Example 4: Extracting Substrings with Parentheses

If you use capturing groups in your regular expression, match() will return additional elements in the resulting array.

let text = "John Doe, age 25"; let result = text.match(/(\w+) (\w+), age (\d+)/); console.log(result); // [ // 'John Doe, age 25', // Full match // 'John', // First capturing group // 'Doe', // Second capturing group // '25', // Third capturing group // index: 0, // input: 'John Doe, age 25', // groups: undefined // ]

Example 5: Using Regular Expressions with Flags

You can create regular expressions with flags using the RegExp constructor, which can be passed to match().

let text = "Hello, world! Hello, universe!"; let regex = new RegExp("hello", "i"); // Case-insensitive match let result = text.match(regex); console.log(result); // [ 'Hello', index: 0, input: 'Hello, world! Hello, universe!', groups: undefined ]

Example 6: String Representation of a Regular Expression

You can also pass a string to match(), but it will be treated as a pattern without any flags.

let text = "The cat sat on the mat."; let result = text.match("cat"); console.log(result); // null (because it's not a valid RegExp, just a string)

Summary:

  • The match() method is used to search for matches against a regular expression in a string.
  • It returns an array of matches or null if no matches are found.
  • With the global flag (g), it returns all matches; without it, it returns details about the first match.
  • Capturing groups in the regular expression allow extraction of specific substrings from the matched results.
  • You can create regular expressions with flags using the RegExp constructor to customize the search behavior.