Bagikan melalui


match Method

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

function match(rgExp : RegExp) : Array

Arguments

  • rgExp
    Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags. Can also be a variable name or string literal containing the regular expression pattern and flags.

Remarks

If the match method does not find a match, it returns null. If it finds a match, match returns an array, and the properties of the global RegExp object are updated to reflect the results of the match.

The array returned by the match method has three properties, input, index and lastIndex. The input property contains the entire searched string. The index property contains the position of the matched substring within the complete searched string. The lastIndex property contains the position following the last character in the last match.

If the global flag (g) is not set, element zero of the array contains the entire match, while elements 1 through n contain any submatches that have occurred within the match. This behavior is identical to the behavior of the exec Method when the global flag is not set on that method. If the global flag is set, elements 0 through n contain all matches that occurred.

Example

The following example illustrates the use of the match method when the global flag (g) is not set.

var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!";

// Create a regular expression to search for an e-mail address.
// The global flag is not included.
// (More sophisticated RegExp patterns are available for
// matching an e-mail address.)
var re = /(\w)@(\w)\.(\w)/;

var result = src.match(re);

// Because the global flag is not included, the entire match is
// in array element 0, and the submatches are in elements 1 through n.
print(result[0]);
for (var index = 1; index < result.length; index+)
{
    print("submatch " + index + ": " + result[index]);
}

// Output:
//  george@contoso.com
//  submatch 1: george
//  submatch 2: contoso
//  submatch 3: com

The following example illustrates the use of the match method when the global flag (g) is set.

var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!";

// Create a regular expression to search for an e-mail address.
// The global flag is included.
var re = /(\w)@(\w)\.(\w)/g;

var result = src.match(re);

// Because the global flag is included, the matches are in
// array elements 0 through n.
for (var index = 0; index < result.length; index+)
{
    print(result[index]);
}

// Output:
//  george@contoso.com
//  someone@example.com

The following lines of code illustrate the use of a string literal with the match method.

var re = /th/i;
var result = "through the pages of this book".match(re);

Requirements

Version 3

Applies To:

String Object

See Also

Reference

exec Method

RegExp Object

Regular Expression Object

replace Method

search Method

test Method

Change History

Date

History

Reason

January 2010

Modified examples.

Information enhancement.

March 2009

Modified examples.

Content bug fix.