match() method retrieves the matches when matching a string against a regular expression.
If the regular expression does not include the
str.match() will return the same result as
RegExp.exec() (en-US). 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 matched substrings rather than match objects. Captured groups are not returned. 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
- if you want to obtain capture groups and the global flag is set, you need to use
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 upper/lower case differences will be ignored.
var str = 'For more information, see Chapter 22.214.171.124'; var re = /see (chapter \d+(\.\d)*)/i; var found = str.match(re); console.log(found); // logs [ 'see Chapter 126.96.36.199', // 'Chapter 188.8.131.52', // '.1', // index: 22, // input: 'For more information, see Chapter 184.108.40.206' ] // 'see Chapter 220.127.116.11' is the whole match. // 'Chapter 18.104.22.168' was captured by '(chapter \d+(\.\d)*)'. // '.1' was the last value captured by '(\.\d)'. // The 'index' property (22) is the zero-based index of the whole match. // The 'input' property is the original string that was parsed.
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); console.log(matches_array); // ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
var str = "Nothing will come of nothing."; str.match(); // returns [""]
When the parameter is a string or a number, it is implicitly converted to a
RegExp by using new RegExp(obj). If it is a positive number with a positive sign,the RegExp() method will ignore the positive sign.
|ECMAScript Language Specification |
BCD tables only load in the browser
flagswas a non standard second argument only available in Gecko : str.match(regexp, flags)
- Starting with Gecko 27 (Firefox 27 / Thunderbird 27 / SeaMonkey 2.24), this method has been adjusted to conform with the ECMAScript specification. When
match()is called with a global regular expression, the
RegExp.lastIndex(en-US) property (if specified) will be reset to
- Starting with Gecko 39 (Firefox 39 / Thunderbird 39 / SeaMonkey 2.36), the non-standard
flagsargument is deprecated and throws a console warning (bug 1142351).
- Starting with Gecko 47 (Firefox 47 / Thunderbird 47 / SeaMonkey 2.44), the non-standard
flagsargument is no longer supported in non-release builds and will soon be removed entirely (bug 1245801).
- Starting with Gecko 49 (Firefox 49 / Thunderbird 49 / SeaMonkey 2.46), the non-standard
flagsargument is no longer supported (bug 1108382).