量詞

量詞指示要匹配的字元或表示式的數量。

試一試

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

另見