match

Used to match a regular expression against a string.

Applies to

String

Syntax

match(regexp)

Parameters

regexp

Name of the regular expression. It can be a variable name or literal.

Description

If you want to execute a global match, or a case insensitive match, include the g (for global) and i (for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with match.

Tip:

If you execute a match simply to find true or false, use String.search or the regular expression test method.

Examples

In the following example, match is used to find 'Chapter' followed by 1 or more numeric characters followed by a decimal point and numeric character 0 or more times. The regular expression includes the i flag so that case will be ignored.

str = "For more information, see Chapter 3.4.5.1";
re = /(chapter \d+(\.\d)*)/i;
found = str.match(re);
Console.Write(found);

This returns the array containing Chapter 3.4.5.1, Chapter 3.4.5.1,.1

'Chapter 3.4.5.1' is the first match and the first value remembered from (Chapter \d+(\.\d)*).

'.1' is the second value remembered from (\.\d).

The following example demonstrates the use of the global and ignore case flags with match.

str = "abcDdcba"; 
newArray = str.match(/d/gi); 
Console.Write(newArray);

The returned array contains D, d.