JavaScript参考手册
字符串 | String
string.match
当一个字符串与一个正则表达式匹配时, match()
方法检索匹配项。
语法
str.match(regexp)
参数
regexp
一个正则表达式对象。如果传入一个非正则表达式对象,则会隐式地使用new RegExp(obj)
将其转换为一个RegExp
。如果你未提供任何参数,直接使用 match() ,那么你会得到一个包含空字符串的Array
:[""] 。
返回值
array
如果字符串匹配到了表达式,会返回一个数组,数组的第一项是进行匹配完整的字符串,之后的项是用圆括号捕获的结果。如果没有匹配到,返回null
描述
如果正则表达式不包含 g
标志,则 str.match()
会返回和 RegExp.exec()
相同的结果。而且返回的 Array
拥有一个额外的 input
属性,该属性包含被解析的原始字符串。另外,还拥有一个 index
属性,该属性表示匹配结果在原字符串中的索引(以0开始)。
如果正则表达式包含 g
标志,则该方法返回一个Array
,它包含所有匹配的子字符串而不是匹配对象。捕获组不会被返回(即不返回index属性和input属性)。如果没有匹配到,则返回 null
。
参看:RegExp
方法
- 如果你需要知道一个字符串是否匹配一个正则表达式
RegExp
,可使用search()
。
- 如果你想要获得捕获组,并且设置了全局标志,你需要用
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' 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
使用全局(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" 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 Latest Draft (ECMA-262)The definition of 'String.prototype.match' in that specification. |
Living Standard |
|
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
flags |
No |
No |
(Yes) — 49 |
No |
No |
No |
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
flags |
No |
No |
No |
No |
No |
No |
No |
字符串 | String相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络