The match()
method retrieves the matches when matching a string against a regular expression.
str.match(regexp)
regexp
obj
is passed, it is implicitly converted to a RegExp
by using new RegExp(obj)
. If you don't give any parameter and use the match() method directly, you will get an Array
with an empty string:[""].An Array
containing the entire match result and any parentheses-captured matched results; null
if there were no matches.
If the regular expression does not include the g
flag, str.match()
will return the same result as RegExp.exec()
. 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 null
.
RegExp
methodsRegExp
, use search()
.RegExp.exec()
instead.RegExp.exec()
instead.match()
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 3.4.5.1'; var re = /see (chapter \d+(\.\d)*)/i; var found = str.match(re); console.log(found); // logs [ 'see Chapter 3.4.5.1', // 'Chapter 3.4.5.1', // '.1', // index: 22, // input: 'For more information, see Chapter 3.4.5.1' ] // 'see Chapter 3.4.5.1' is the whole match. // 'Chapter 3.4.5.1' 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.
match()
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']
match() with no parameter
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.
var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.", str2 = "My grandfather is 65 years old and My grandmother is 63 years old.", str3 = "The contract was declared null and void."; str1.match("number"); // "number" is a string. returns ["number"] str1.match(NaN); // the type of NaN is the number. returns ["NaN"] str1.match(Infinity); // the type of Infinity is the number. returns ["Infinity"] str1.match(+Infinity); // returns ["Infinity"] str1.match(-Infinity); // returns ["-Infinity"] str2.match(65); // returns ["65"] str2.match(+65); // A number with a positive sign. returns ["65"] str3.match(null); // returns ["null"]
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) The definition of 'String.prototype.match' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.match' in that specification. | Standard | |
ECMAScript 2017 Draft (ECMA-262) The definition of 'String.prototype.match' in that specification. | Draft |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
match()
is called with a global regular expression, the RegExp.lastIndex
property (if specified) will be reset to 0
(bug 501739).flags
argument is deprecated and throws a console warning (bug 1142351).flags
argument is no longer supported in non-release builds and will soon be removed entirely (bug 1245801).flags
argument is no longer supported (bug 1108382).
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match