在 ES10 中,新增了一个非常实用的方法 String.prototype.matchAll(),它可以配合正则表达式使用,用于获取字符串中所有匹配正则表达式的子串。这个方法的出现,可以让我们更方便地处理字符串中的匹配问题,提高代码的可读性和可维护性。
String.prototype.matchAll() 的使用方法
String.prototype.matchAll() 方法的使用方法与 String.prototype.match() 方法类似,都是用于字符串匹配的。不同的是,matchAll() 方法返回的是一个迭代器,可以遍历字符串中所有匹配正则表达式的子串,而 match() 方法只返回第一个匹配的子串。
下面是 matchAll() 方法的使用示例:
const str = 'The quick brown fox jumps over the lazy dog.';
const regex = /[a-z]/g;
const matches = str.matchAll(regex);
for (const match of matches) {
console.log(match);
}输出结果为:
// www.javascriptcn.com code example ["h", index: 1, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["e", index: 2, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["q", index: 4, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["u", index: 5, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["i", index: 7, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["c", index: 8, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["k", index: 9, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["b", index: 11, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["r", index: 12, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["o", index: 14, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["w", index: 15, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["n", index: 17, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["f", index: 19, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["o", index: 20, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["x", index: 22, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["j", index: 24, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["u", index: 25, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["m", index: 27, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["p", index: 28, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["s", index: 30, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["o", index: 31, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["v", index: 33, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["e", index: 34, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["r", index: 36, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["t", index: 38, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["h", index: 40, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["e", index: 41, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["l", index: 43, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["a", index: 44, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["z", index: 46, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["y", index: 48, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["d", index: 50, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["o", index: 52, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["g", index: 54, input: "The quick brown fox jumps over the lazy dog.", groups: undefined] ["", index: 55, input: "The quick brown fox jumps over the lazy dog.", groups: undefined]
可以看到,matchAll() 方法返回的是一个迭代器,可以通过 for...of 循环遍历其中的每一个匹配结果。每一个匹配结果都是一个数组,包含了匹配到的子串和相关信息。
String.prototype.matchAll() 的指导意义
String.prototype.matchAll() 方法的出现,可以让我们更方便地处理字符串中的匹配问题。在以前,如果需要获取字符串中所有匹配正则表达式的子串,需要使用 match() 方法配合正则表达式的 g 标志,然后使用循环遍历返回的数组。这种方式虽然能够达到目的,但是代码可读性不高,而且不够简洁。
使用 matchAll() 方法,可以将这个过程简化为一行代码,同时还可以提高代码的可读性和可维护性。因此,我们在处理字符串匹配问题时,应该优先考虑使用 matchAll() 方法。
示例代码
下面是一个使用 matchAll() 方法的示例代码,用于统计一个字符串中所有单词出现的次数:
// www.javascriptcn.com code example
const str = 'The quick brown fox jumps over the lazy dog.';
const regex = /\b\w+\b/g;
const matches = str.matchAll(regex);
const wordCount = {};
for (const match of matches) {
const word = match[0];
wordCount[word] = (wordCount[word] || 0) + 1;
}
console.log(wordCount);输出结果为:
// www.javascriptcn.com code example
{
"The": 1,
"quick": 1,
"brown": 1,
"fox": 1,
"jumps": 1,
"over": 1,
"the": 1,
"lazy": 1,
"dog": 1
}可以看到,这个示例代码使用 matchAll() 方法和正则表达式,可以很方便地统计一个字符串中所有单词出现的次数。如果不使用 matchAll() 方法,这个过程可能需要写更多的代码,并且不够简洁。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/67d95b6da941bf71340f2633