Quantifiers

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 exactly n occurrences of the preceding element
  • {n,} : Matches n or more occurrences of the preceding element
  • {n,m} : Matches between n and m occurrences of the preceding element

Example: Using Quantifiers

Let’s look at some examples of how these quantifiers work:

  1. Asterisk (*) Quantifier
javascript
	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')
  1. Plus (+) Quantifier
javascript
	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')
  1. Question Mark (?) Quantifier
javascript
	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')
  1. Curly Braces ({n}) Quantifier
javascript
	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)
  1. Curly Braces ({n,}) Quantifier
javascript
	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)
  1. Curly Braces ({n,m}) Quantifier
javascript
	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.

  1. Greedy Quantifier
javascript
	const regexGreedy = /a{2,4}/;
	const str = 'aaaaa';
	console.log(str.match(regexGreedy)); // ['aaaa'] (matches the longest possible string)
  1. Lazy Quantifier
javascript
	const regexLazy = /a{2,4}?/;
	const str = 'aaaaa';
	console.log(str.match(regexLazy)); // ['aa'] (matches the shortest possible string)

Exercises

  1. Write a regular expression that matches a string containing exactly three digits.
  2. Create a Regex pattern that matches a string with at least two and at most five lowercase letters.
  3. 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.