Quantifiers — Regex

A quantifier sets how many times the thing right in front of it repeats: ? 0 or 1, * 0 or more, + 1 or more, {n} exactly n, {n,m} between n and m, {n,} at…

In the last lesson, [0-9] took only one digit, so $85.00 broke into four results. To get the whole price, a pattern needs a way to say "there can be many digits".

Mrs. Ratna's PO form has rules like that: "tarts, at least 1 jar", "a greeting card, optional", "at most 5 jars per person". The rules don't name the item, only the amount, and each one applies to the item named right before it. In a regex, a rule about amounts is called a quantifier: it sets how many times the thing right in front of it may repeat. Without a quantifier, every part of a pattern matches exactly once.

A quantifier repeats the one thing in front of it

Quantifier Meaning Example from the chat
? 0 or 1 time (optional) -? a dash may be there
* 0 or more times 07\d*
+ 1 or more times \d+
{4} exactly 4 times PO-\d{4}
{10,13} 10 to 13 times \d{10,13}
{2,} at least 2 times \d{2,}
console.log(chat.match(/\$\d/g));        // ['$8', '$1', '$1']
console.log(chat.match(/\$\d+/g));       // ['$85', '$165', '$170']
console.log(chat.match(/\d+ dollars/g)); // ['90 dollars', '255 dollars']

?, * and + in phone numbers

console.log(chat.match(/07\d+/g)); // ['07700', '07700900461']
console.log(chat.match(/07\d*/g)); // ['07', '07', '07700', '07', '07', '07', '07700900461', '07']
console.log(chat.match(/07\d{3}-?\d{3}-?\d{3}/g)); // ['07700-900-123', '07700900461']

Exact counts: {n}, {n,m}, {n,}

console.log(chat.match(/07\d{9}/g));    // ['07700900461']
console.log(chat.match(/\d{10,13}/g));  // ['1234567890', '07700900461']
console.log("aabbb".match(/ab{2,3}/)[0]); // 'abbb'

Greedy and lazy

The chat's first line has three bold texts, WhatsApp style: *Tart*, *Cheese Sticks*, *Snowballs*. The pattern \*.+\* means "an asterisk, anything, an asterisk". Does it take one text, all three separately, or one long piece? Hold on to your guess and read on.

Quantifiers are greedy: they take as much as they can first, then back off bit by bit if the rest of the pattern doesn't fit.

console.log(chat.match(/\*.+\*/g));  // ['*Tart* $85/jar, *Cheese Sticks* 90 dollars, *Snowballs*']
console.log(chat.match(/\*.+?\*/g)); // ['*Tart*', '*Cheese Sticks*', '*Snowballs*']
console.log("aXXbXX".match(/X+/g));  // ['XX', 'XX']
console.log("aXXbXX".match(/X+?/g)); // ['X', 'X', 'X', 'X']

⚠️ Quantifiers that grab too much, or the wrong thing

TL;DR: A quantifier sets how many times the thing right in front of it repeats: ? 0 or 1, * 0 or more, + 1 or more, {n} exactly n, {n,m} between n and m, {n,} at least n. Quantifiers are greedy: "aabbb".match(/ab{2,3}/)[0] gives abbb, and \*.+\* swallows everything from the first asterisk to the last. Add ? for lazy: \*.+?\* takes each bold text, X+? one X per result. * can match zero characters, so test() with \d* is always true.

⚡ A quantifier repeats the one thing in front of it

Write:

console.log(chat.match(/\$\d+/g)); // ['$85', '$165', '$170']

Meaning: ? 0 or 1, * 0 or more, + 1 or more, {n} exactly n, {n,m} between n and m. Without the +, \d takes only one digit: $8.

⚡ Greedy: take as much as it can

Write:

console.log("aabbb".match(/ab{2,3}/)[0]);
Guess first: 2 or 3 b's are allowed. Is the result abb or abbb?

abbb. Quantifiers are greedy: they take as much as they can first, and only back off when the rest of the pattern doesn't fit.

Meaning: A quantifier starts from the most. \*.+\* swallows one long piece, from the first asterisk on the line to the last.

⚡ Lazy: add a ?

Write:

console.log(chat.match(/\*.+?\*/g)); // ['*Tart*', '*Cheese Sticks*', '*Snowballs*']

Meaning: A ? after a quantifier makes it lazy: it takes as little as possible, so each bold text comes out on its own.

What you will learn