totn JavaScript

JavaScript: String match() method

This JavaScript tutorial explains how to use the string method called match() with syntax and examples.

Description

In JavaScript, match() is a string method that is used to find matches based on regular expression matching. Because the match() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the match() method is:

string.match(regexp);

Parameters or Arguments

regexp

It is a RegExp object that contains the pattern to match. It can be a combination of the following:

Value Description
^ Matches the beginning of a string. If used with a match_parameter of 'm', it matches the start of a line anywhere within expression.
$ Matches the end of a string. If used with a match_parameter of 'm', it matches the end of a line anywhere within expression.
* Matches zero or more occurrences.
+ Matches one or more occurrences.
? Matches zero or one occurrence.
. Matches any character except NULL.
| Used like an "OR" to specify more than one alternative.
[ ] Used to specify a matching list where you are trying to match any one of the characters in the list.
[^ ] Used to specify a nonmatching list where you are trying to match any character except for the ones in the list.
( ) Used to group expressions as a subexpression.
\b Matches a word boundary
\B Matches a non-word boundary
{m} Matches m times.
{m,} Matches at least m times.
{m,n} Matches at least m times, but no more than n times.
\n n is a number between 1 and 9. Matches the nth subexpression found within ( ) before encountering \n.
[..] Matches one collation element that can be more than one character.
[::] Matches character classes.
[==] Matches equivalence classes.
\d Matches a digit character.
\D Matches a nondigit character.
\w Matches a word character.
\W Matches a nonword character.
\s Matches a whitespace character.
\S matches a non-whitespace character.
\t matches a horizontal tab character.
\v matches a vertical tab character.
\r matches a carriage return character.
\f matches a form feed character.
\n matches a line feed character.
[\b] matches a backspace character.
\0 matches a NUL character.
*? Matches the preceding pattern zero or more occurrences.
+? Matches the preceding pattern one or more occurrences.
?? Matches the preceding pattern zero or one occurrence.
{n}? Matches the preceding pattern n times.
{n,}? Matches the preceding pattern at least n times.
{n,m}? Matches the preceding pattern at least n times, but not more than m times.

Returns

The match() method returns an array of the match results (see example below as the contents of the array will vary depending on single or global match).

If no match is found, the match() method will return null.

Note

  • The match() method does not change the value of the original string.
  • The match() method will return different results depending on whether it is performing a global match as specified by the g attribute. See the example below for an explanation.

Example

Let's take a look at an example of how to use the match() method in JavaScript.

Single Match

You can use the match() method to search for the first occurrence of a regular expression pattern.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.match(/[A-Z]/));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the match() method of the totn_string to find matches based on a regular expression.

We have written the output of the match() method to the web browser console log, for demonstration purposes, to show what the match() method returns.

The following will be output to the web browser console log:

["T", index: 0, input: "TechOnTheNet", groups: undefined]

In this example, the match() method performs a search for the first occurrence of an uppercase character. It returned an array containing information about the match that was found. Element 0 of the array contained the single matched value, element 1 contained the index position of the matched value, element 2 contained the input string that the match was performed on, and element 3 contained the groups.

Global Match (Search for Multiple Matches)

You can also use the match() method to search for all matches of a regular expression pattern. This is done by performing a global match as specified by the g attribute.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.match(/[A-Z]/g));

The following will be output to the web browser console log:

["T", "O", "T", "N"]

In this example, the match() method performed a global match and searched for all occurrences of uppercase characters. This was done by specifying the g attribute at the end of the regular expression.

This time the match() returned an array of the all of the matched values. It found four occurrences of uppercase letters, each of which is an element in the returned array.