試一試
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 |
瀏覽器相容性
載入中…