量词
量词表示要匹配的字符或表达式的数量。
尝试一下
类型
Characters | Meaning |
---|---|
x* |
将前面的项“x”匹配 0 次或更多次。例如,/bo*/匹配“A ghost booooed”中的“boooo”和“A bird warbled”中的“b”,但在“A goat grunt”中没有匹配。 |
x+ |
将前一项“x”匹配 1 次或更多次。等价于{1,}。例如,/a+/匹配“candy”中的“a”和“caaaaaaandy”中的“a”。 |
x? |
将前面的项“x”匹配 0 或 1 次。例如,/e?le?/匹配 angel 中的 el 和 angle 中的 le。 如果立即在任何量词*、+、?或{}之后使用,则使量词是非贪婪的 (匹配最小次数),而不是默认的贪婪的 (匹配最大次数)。 |
x{n} |
其中“n”是一个正整数,与前一项“x”的 n 次匹配。例如, |
x{n,} |
其中,“n”是一个正整数,与前一项“x”至少匹配“n”次。例如, |
x{n,m} |
其中,“n”是 0 或一个正整数,“m”是一个正整数,而 m > n 至少与前一项“x”匹配,最多与“m”匹配。例如,/a{1,3}/不匹配“cndy”中的“a”,“candy”中的“a”,“caandy”中的两个“a”,以及“caaaaaaandy”中的前三个“a”。注意,当匹配“caaaaaaandy”时,匹配的是“aaa”,即使原始字符串中有更多的“a”。 |
|
默认情况下,像
|
举例说明
重复模式
var wordEndingWithAs = /\w+a+/;
var delicateMessage = "This is Spartaaaaaaa";
console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
计算字符集
var singleLetterWord = /\b\w\b/g;
var notSoLongWord = /\b\w{1,6}\b/g;
var loooongWord = /\b\w{13,}\b/g;
var sentence = "Why do I have to learn multiplication table?";
console.table(sentence.match(singleLetterWord)); // ["I"]
console.table(sentence.match(notSoLongWord)); // [ "Why", "do", "I", "have", "to", "learn", "table" ]
console.table(sentence.match(loooongWord)); // ["multiplication"] 可选可选字符
可选字符
var britishText = "He asked his neighbour a favour.";
var americanText = "He asked his neighbor a favor.";
var 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"]
贪婪 与 非贪婪的
var text = "I must be getting somewhere near the centre of the earth.";
var greedyRegexp = /[\w ]+/;
// [\w ] a letter of the latin alphabet or a whitespace
// + one or several times
console.log(text.match(greedyRegexp)[0]);
// "I must be getting somewhere near the centre of the earth"
// almost all of the text matches (leaves out the dot character)
var nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
console.log(text.match(nonGreedyRegexp));
// "I"
// The match is the smallest one possible