首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

string.match

当一个字符串与一个正则表达式匹配时,?match()方法检索匹配项。

语法

代码语言:javascript
复制
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标志,因此大小写会被忽略。

代码语言:javascript
复制
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 的所有字母将会作为一个数组的元素返回。

代码语言:javascript
复制
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(),不传参数

代码语言:javascript
复制
var str = "Nothing will come of nothing.";

str.match();   // returns [""]

一个非正则表达式对象作为参数

当参数是一个字符串或一个数字,它会使用new RegExp(obj)来隐式转换成一个RegExp。如果它是一个有正号的正数,RegExp() 方法将忽略正号。

代码语言:javascript
复制
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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com