试一试
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"]
语法
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]()。
- 如果你需要知道一个字符串是否与
RegExp匹配,请使用RegExp.prototype.test()。 - 如果你只想获取找到的第一个匹配项,可以考虑使用
RegExp.prototype.exec()。 - 如果你想获取捕获组并且设置了全局标志,你需要使用
RegExp.prototype.exec()或String.prototype.matchAll()。
有关将正则表达式传递给 match() 时其语义的更多信息,请参阅 RegExp.prototype[Symbol.match]()。
示例
使用 match()
在下面的示例中,match() 用于查找后跟一个或多个数字字符、一个小数点和零个或多个数字字符的“Chapter”。
正则表达式包含 i 标志,因此会忽略大小写差异。
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(包括大小写)的字母,每个字母都是数组中的一个独立元素。
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']
注意:另请参阅 String.prototype.matchAll() 和 带标志的高级搜索。
使用命名捕获组
在支持命名捕获组的浏览器中,以下代码将“fox”或“cat”捕获到名为 animal 的组中。
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()
const str = "Nothing will come of nothing.";
str.match(); // returns [""]
使用具有未实现 [Symbol.match]() 的非 RegExp 对象
如果一个对象有一个 Symbol.match 方法,它就可以被用作自定义匹配器。Symbol.match 的返回值将成为 match() 的返回值。
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)。
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"]
如果不正确地转义特殊字符,这可能会产生意外的结果。
console.log("123".match("1.3")); // [ "123" ]
这是一个匹配,因为在正则表达式中 . 匹配任何字符。为了使其仅匹配小数点字符,你需要转义输入。
console.log("123".match("1\\.3")); // null
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-string.prototype.match |
浏览器兼容性
加载中…