String.prototype.match()

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

match() 方法用于检索字符串对象匹配 正则表达式的结果。

试一试

const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const regex = /[A-Z]/g;
const found = paragraph.match(regex);

console.log(found);
// Expected output: Array ["T", "I"]

语法

js
match(regexp)

参数

regexp

一个正则表达式对象,或者任何拥有 Symbol.match 方法的对象。

如果 regexp 不是 RegExp 对象并且不具有 Symbol.match 方法,则它会被隐式转换为 RegExp 对象,使用 new RegExp(regexp)

如果不提供任何参数,直接调用 match() 方法,会得到一个包含空字符串的 Array[""],因为这等同于调用 match(/(?:)/)

返回值

一个 Array,其内容取决于全局标志 (g) 的存在与否,如果没有找到匹配项,则返回 null

  • 如果使用了 g 标志,将返回所有与完整正则表达式匹配的结果,但捕获组不包含在内。
  • 如果未使用 g 标志,则仅返回第一个完整匹配项及其相关的捕获组。在这种情况下,match() 将返回与 RegExp.prototype.exec() 相同的结果(一个带有额外属性的数组)。

描述

String.prototype.match 的实现除了调用参数的 Symbol.match 方法并将字符串作为第一个参数外,并没有做太多事情。实际的实现来自 RegExp.prototype[Symbol.match]()

有关将正则表达式传递给 match() 时其语义的更多信息,请参阅 RegExp.prototype[Symbol.match]()

示例

使用 match()

在下面的示例中,match() 用于查找后跟一个或多个数字字符、一个小数点和零个或多个数字字符的“Chapter”。

正则表达式包含 i 标志,因此会忽略大小写差异。

js
const str = "For more information, see Chapter 3.4.5.1";
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);

console.log(found);
// [
//   'see Chapter 3.4.5.1',
//   'Chapter 3.4.5.1',
//   '.1',
//   index: 22,
//   input: 'For more information, see Chapter 3.4.5.1',
//   groups: undefined
// ]

在上面的匹配结果中,'see Chapter 3.4.5.1' 是整个匹配。'(chapter \d+(\.\d)*)' 捕获了 'Chapter 3.4.5.1''(\.\d)' 捕获的最后一个值是 '.1'index 属性(22)是整个匹配的零基索引。input 属性是经过解析的原始字符串。

使用全局和忽略大小写标志与 match()

下面的示例演示了在 match() 中使用全局标志和忽略大小写标志。返回所有从 A 到 E(包括大小写)的字母,每个字母都是数组中的一个独立元素。

js
const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const regexp = /[a-e]/gi;
const matches = str.match(regexp);

console.log(matches);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

使用命名捕获组

在支持命名捕获组的浏览器中,以下代码将“fox”或“cat”捕获到名为 animal 的组中。

js
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";

const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);
console.log(found.groups); // {animal: "fox"}

在没有参数的情况下使用 match()

js
const str = "Nothing will come of nothing.";

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

使用具有未实现 [Symbol.match]() 的非 RegExp 对象

如果一个对象有一个 Symbol.match 方法,它就可以被用作自定义匹配器。Symbol.match 的返回值将成为 match() 的返回值。

js
const str = "Hmm, this is interesting.";

str.match({
  [Symbol.match](str) {
    return ["Yes, it's interesting."];
  },
}); // returns ["Yes, it's interesting."]

将非 RegExp 作为参数

regexp 参数是字符串或数字时,它会被隐式转换为 RegExp 对象,使用 new RegExp(regexp)

js
const str1 =
  "All numbers except NaN satisfy <= Infinity and >= -Infinity in JavaScript.";
const str2 =
  "My grandfather is 65 years old and My grandmother is 63 years old.";
const 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"]
str2.match(65); // returns ["65"]
str3.match(null); // returns ["null"]

如果不正确地转义特殊字符,这可能会产生意外的结果。

js
console.log("123".match("1.3")); // [ "123" ]

这是一个匹配,因为在正则表达式中 . 匹配任何字符。为了使其仅匹配小数点字符,你需要转义输入。

js
console.log("123".match("1\\.3")); // null

规范

规范
ECMAScript® 2026 语言规范
# sec-string.prototype.match

浏览器兼容性

另见