match()
方法检索返回一个字符串匹配正则表达式的的结果。
str.match(regexp)
regexp
new RegExp(obj)
将其转换为一个 RegExp
。如果你没有给出任何参数并直接使用match() 方法 ,你将会得到一 个包含空字符串的 Array
:[""] 。Array
),但不会返回捕获组,或者未匹配 null
。Array
)。 在这种情况下,返回的项目将具有如下所述的其他属性,或者未匹配 null
。如上所述,匹配的结果包含如下所述的附加特性。
groups
: 一个捕获组数组 或 undefined
(如果没有定义命名捕获组)。index
: 匹配的结果的开始位置input
: 搜索的字符串.Array
,其内容取决于global(g
)标志的存在与否,如果未找到匹配则为null
。如果正则表达式不包含 g
标志,str.match()
将返回与 RegExp.exec()
. 相同的结果。
RegExp
方法RegExp
,可使用 test()
。RegExp.exec()
。RegExp.exec()
。match
在下例中,使用 match
查找 "Chapter
" 紧跟着 1 个或多个数值字符,再紧跟着一个小数点和数值字符 0 次或多次。正则表达式包含 i
标志,因此大小写会被忽略。
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' 是整个匹配。
// 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。
// '.1' 是被'(\.\d)'捕获的最后一个值。
// 'index' 属性(22) 是整个匹配从零开始的索引。
// 'input' 属性是被解析的原始字符串。
match
使用全局(global)和忽略大小写(ignore case)标志下例展示了 match
使用 global 和 ignore case 标志。A-E、a-e 的所有字母将会作为一个数组的元素返回。
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(),不传参数
var str = "Nothing will come of nothing.";
str.match(); // returns [""]
当参数是一个字符串或一个数字,它会使用new RegExp(obj)来隐式转换成一个 RegExp
。如果它是一个有正号的正数,RegExp() 方法将忽略正号。
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" 是字符串。返回["number"]
str1.match(NaN); // NaN的类型是number。返回["NaN"]
str1.match(Infinity); // Infinity的类型是number。返回["Infinity"]
str1.match(+Infinity); // 返回["Infinity"]
str1.match(-Infinity); // 返回["-Infinity"]
str2.match(65); // 返回["65"]
str2.match(+65); // 有正号的number。返回["65"]
str3.match(null); // 返回["null"]
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) String.prototype.match |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) String.prototype.match |
Standard | |
ECMAScript Latest Draft (ECMA-262) String.prototype.match |
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()时,
RegExp.lastIndex
属性(如果指定)会重置为 0
(bug 501739)。