Quantifiers
Quantifiers indicate numbers of characters or expressions to match.
Try it
Types
Note: In the following, item refers not only to singular characters, but also includes character classes and groups and backreferences.
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 non-negative integer, matches exactly "n" occurrences of
the preceding item "x". For example, |
x{n,} |
Where "n" is a non-negative integer, matches at least "n" occurrences of
the preceding item "x". For example, |
x{n,m} |
Where "n" and "m" are non-negative integers and |
|
By default quantifiers like
|
Examples
Repeated pattern
In this example, we match one or more word characters with \w+
, then one or more characters "a" with a+
, and finally end at a word boundary with \b
.
const wordEndingWithAs = /\w+a+\b/;
const delicateMessage = "This is Spartaaaaaaa";
console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
Counting characters
In this example, we match words that have a single letter, words that have between 2 and 6 letters, and words that have 13 or more letters.
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"]
Optional character
In this example, we match words that either end with "our" or "or".
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"]
Greedy versus non-greedy
In this example, we match one or more word characters or spaces with [\w ]+
and [\w ]+?
. The first one is greedy and the second one is non-greedy. Note how the second one stops as soon as it meets the minimal requirement.
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
See also
- Regular expressions guide
- Character classes guide
- Assertions guide
- Groups and backreferences guide
RegExp
- Regular expressions reference
- Quantifier:
*
,+
,?
,{n}
,{n,}
,{n,m}