Used to retrieve the matches when matching a
string against a regular expression.
|ECMAScript Edition||ECMAScript 3rd Edition|
var array = string.match(regexp);
A regular expression object. If a non-RegExp object
objis passed, it is implicitly converted to a RegExp by using
If the regular expression does not include the
g flag, returns the same result as
regexp.exec(string). The returned
Array has an extra
input property, which contains the original string that was parsed. In addition, it has an
index property, which represents the zero-based index of the match in the string.
If the regular expression includes the
g flag, the method returns an
Array containing all matches. If there were no matches, the method returns
- If you need to know if a string matches a regular expression
- If you only want the first match found, you might want to use
- See §22.214.171.124 of the ECMA-262 specification.
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.
var str = "For more information, see Chapter 126.96.36.199"; var re = /(chapter \d+(\.\d)*)/i; var found = str.match(re); alert(found);
This returns the array containing Chapter 188.8.131.52,Chapter 184.108.40.206,.1
Chapter 220.127.116.11" is the first match and the first value remembered from
.1" is the second value remembered from
Example: Using global and ignore case flags with
The following example demonstrates the use of the global and ignore case flags with
match. All letters A through E and a through e are returned, each its own element in the array
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var regexp = /[A-E]/gi; var matches_array = str.match(regexp); alert(matches_array);
matches_array now equals
['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']