量词

量词指示要匹配的字符或表达式的数量。

试一试

const ghostSpeak = "booh boooooooh";
const regexpSpooky = /bo{3,}h/;
console.log(ghostSpeak.match(regexpSpooky));
// Expected output: Array ["boooooooh"]

const modifiedQuote = "[He] ha[s] to go read this novel [Alice in Wonderland].";
const regexpModifications = /\[.*?\]/g;
console.log(modifiedQuote.match(regexpModifications));
// Expected output: Array ["[He]", "[s]", "[Alice in Wonderland]"]

const regexpTooGreedy = /\[.*\]/g;
console.log(modifiedQuote.match(regexpTooGreedy));
// Expected output: Array ["[He] ha[s] to go read this novel [Alice in Wonderland]"]

类型

注意:在下文中,不仅指单个字符,还包括字符类以及分组和反向引用

字符 含义
x*

匹配前面项“x”0 次或更多次。例如,/bo*/ 匹配“A ghost booooed”中的“boooo”和“A bird warbled”中的“b”,但在“A goat grunted”中不匹配任何内容。

x+

匹配前面项“x”1 次或更多次。等同于 {1,}。例如,/a+/ 匹配“candy”中的“a”和“caaaaaaandy”中所有的“a”。

x?

匹配前面项“x”0 次或 1 次。例如,/e?le?/ 匹配“angel”中的“el”和“angle”中的“le”。

如果紧接在量词 *+?{} 之后使用,则使量词变为非贪婪(匹配最小次数),而不是默认的贪婪(匹配最大次数)。

x{n}

其中“n”是非负整数,精确匹配前面项“x”的“n”个出现。例如,/a{2}/ 不匹配“candy”中的“a”,但匹配“caandy”中所有的“a”,以及“caaandy”中的前两个“a”。

x{n,}

其中“n”是非负整数,匹配前面项“x”的至少“n”个出现。例如,/a{2,}/ 不匹配“candy”中的“a”,但匹配“caandy”和“caaaaaaandy”中所有的“a”。

x{n,m}

其中“n”和“m”是非负整数且 m >= n,匹配前面项“x”的至少“n”次和最多“m”次出现。例如,/a{1,3}/ 在“cndy”中不匹配任何内容,匹配“candy”中的“a”,“caandy”中的两个“a”,以及“caaaaaaandy”中的前三个“a”。请注意,当匹配“caaaaaaandy”时,匹配的是“aaa”,即使原始字符串中有更多的“a”。

x*?
x+?
x??
x{n}?
x{n,}?
x{n,m}?

默认情况下,像 *+ 这样的量词是“贪婪的”,这意味着它们会尝试匹配尽可能多的字符串。量词后面的 ? 字符使量词变为“非贪婪的”:这意味着它会在找到匹配项后立即停止。例如,给定一个字符串“some <foo> <bar> new </bar> </foo> thing”

  • /<.*>/ 将匹配“<foo> <bar> new </bar> </foo>”
  • /<.*?>/ 将匹配“<foo>”

示例

重复模式

在这个例子中,我们用 \w+ 匹配一个或多个单词字符,然后用 a+ 匹配一个或多个字符“a”,最后用 \b 结束于一个单词边界。

js
const wordEndingWithAs = /\w+a+\b/;
const delicateMessage = "This is Spartaaaaaaa";

console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]

计数字符

在这个例子中,我们匹配只有一个字母的单词、有 2 到 6 个字母的单词以及有 13 个或更多字母的单词。

js
const singleLetterWord = /\b\w\b/g;
const notSoLongWord = /\b\w{2,6}\b/g;
const longWord = /\b\w{13,}\b/g;

const sentence = "Why do I have to learn multiplication table?";

console.table(sentence.match(singleLetterWord)); // ["I"]
console.table(sentence.match(notSoLongWord)); // [ "Why", "do", "have", "to", "learn", "table" ]
console.table(sentence.match(longWord)); // ["multiplication"]

可选字符

在这个例子中,我们匹配以“our”或“or”结尾的单词。

js
const britishText = "He asked his neighbour a favour.";
const americanText = "He asked his neighbor a favor.";

const regexpEnding = /\w+ou?r/g;
// \w+ One or several letters
// o   followed by an "o",
// u?  optionally followed by a "u"
// r   followed by an "r"

console.table(britishText.match(regexpEnding));
// ["neighbour", "favour"]

console.table(americanText.match(regexpEnding));
// ["neighbor", "favor"]

贪婪与非贪婪

在这个例子中,我们用 [\w ]+[\w ]+? 匹配一个或多个单词字符或空格。第一个是贪婪的,第二个是非贪婪的。请注意第二个是如何在满足最小要求后立即停止的。

js
const text = "I must be getting somewhere near the center of the earth.";
const greedyRegexp = /[\w ]+/;

console.log(text.match(greedyRegexp)[0]);
// "I must be getting somewhere near the center of the earth"
// almost all of the text matches (leaves out the dot character)

const nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
console.log(text.match(nonGreedyRegexp));
// "I"
// The match is the smallest one possible

另见