Quantifiers
Understanding Quantifiers
Quantifiers in Regex specify how many times an element (character, group, or character class) should be matched. They are placed after the element they apply to.
Common Quantifiers
Here are some of the most commonly used quantifiers:
*
: Matches 0 or more occurrences of the preceding element+
: Matches 1 or more occurrences of the preceding element?
: Matches 0 or 1 occurrence of the preceding element{n}
: Matches exactlyn
occurrences of the preceding element{n,}
: Matchesn
or more occurrences of the preceding element{n,m}
: Matches betweenn
andm
occurrences of the preceding element
Example: Using Quantifiers
Let’s look at some examples of how these quantifiers work:
- Asterisk (
*
) Quantifier
const regexAsterisk = /a*/;
console.log(regexAsterisk.test('aaa')); // true (matches 'aaa')
console.log(regexAsterisk.test('b')); // true (matches '' because 0 occurrences of 'a' is allowed)
console.log(regexAsterisk.test('abc')); // true (matches 'a')
- Plus (
+
) Quantifier
const regexPlus = /a+/;
console.log(regexPlus.test('aaa')); // true (matches 'aaa')
console.log(regexPlus.test('b')); // false (no match because at least 1 'a' is required)
console.log(regexPlus.test('abc')); // true (matches 'a')
- Question Mark (
?
) Quantifier
const regexQuestion = /a?/;
console.log(regexQuestion.test('aaa')); // true (matches 'a')
console.log(regexQuestion.test('b')); // true (matches '' because 0 occurrences of 'a' is allowed)
console.log(regexQuestion.test('abc')); // true (matches 'a')
- Curly Braces (
{n}
) Quantifier
const regexExact = /a{3}/;
console.log(regexExact.test('aaa')); // true (matches 'aaa')
console.log(regexExact.test('aa')); // false (no match because exactly 3 'a's are required)
console.log(regexExact.test('aaaa')); // true (matches 'aaa' and the remaining 'a' is ignored)
- Curly Braces (
{n,}
) Quantifier
const regexAtLeast = /a{2,}/;
console.log(regexAtLeast.test('aaa')); // true (matches 'aaa')
console.log(regexAtLeast.test('aa')); // true (matches 'aa')
console.log(regexAtLeast.test('a')); // false (no match because at least 2 'a's are required)
- Curly Braces (
{n,m}
) Quantifier
const regexRange = /a{2,4}/;
console.log(regexRange.test('aaa')); // true (matches 'aaa')
console.log(regexRange.test('aa')); // true (matches 'aa')
console.log(regexRange.test('aaaaa')); // true (matches 'aaaa' and the remaining 'a' is ignored)
console.log(regexRange.test('a')); // false (no match because at least 2 'a's are required)
Greedy vs. Lazy Quantifiers
By default, quantifiers are greedy, meaning they try to match as many characters as possible. You can make them lazy (non-greedy) by adding a question mark ?
after the quantifier.
- Greedy Quantifier
const regexGreedy = /a{2,4}/;
const str = 'aaaaa';
console.log(str.match(regexGreedy)); // ['aaaa'] (matches the longest possible string)
- Lazy Quantifier
const regexLazy = /a{2,4}?/;
const str = 'aaaaa';
console.log(str.match(regexLazy)); // ['aa'] (matches the shortest possible string)
Exercises
- Write a regular expression that matches a string containing exactly three digits.
- Create a Regex pattern that matches a string with at least two and at most five lowercase letters.
- Write a regular expression that matches a string with zero or more occurrences of the letter ‘b’ followed by exactly one ‘c’.
Quantifiers are powerful tools for specifying the number of occurrences of elements in your Regex patterns. In the next lesson, we will explore anchors and boundaries, which help you define the position of your matches within strings.