Quantifiers - JavaScript 编辑
Quantifiers indicate numbers of characters or expressions to match.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Types
The following table is also duplicated on this cheatsheet. Do not forget to edit it as well, thanks!Note: In the following, item refers not only to singular characters, but also includes character classes, Unicode property escapes, groups and ranges.
Characters | Meaning |
---|---|
x* | Matches the preceding item "x" 0 or more times. For example, |
x+ | Matches the preceding item "x" 1 or more times. Equivalent to |
x? | Matches the preceding item "x" 0 or 1 times. For example, If used immediately after any of the quantifiers |
x{n} | Where "n" is a positive integer, matches exactly "n" occurrences of the preceding item "x". For example, |
x{n,} | Where "n" is a positive integer, matches at least "n" occurrences of the preceding item "x". For example, |
x{n,m} | Where "n" is 0 or a positive integer, "m" is a positive integer, and |
| By default quantifiers like
|
Examples
Repeated pattern
var wordEndingWithAs = /\w+a+\b/;
var delicateMessage = "This is Spartaaaaaaa";
console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
Counting characters
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"]
Optional character
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"]
Greedy versus non-greedy
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
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'RegExp: Quantifiers' in that specification. |
Browser compatibility
For browser compatibility information, check out the main Regular Expressions compatibility table.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论