Special Characters: Quantifiers

Get introduced to the different types of quantifiers and their examples.

You now understand what a character class is, but if you try the examples from the previous lesson in regex101.com, you’ll see how you’re only matching one character. Here is where quantifiers come in handy. The point of quantifiers is to let the parser understand how many repetitions of the class can happen inside a given match.

Take into account that a character class encompasses several different characters, and a quantifier bigger than one means any of those can happen more than once.

Examples of quantifiers

Quantifiers, just like character classes, allow you to be fuzzy with the specifics, helping you set up dynamic patterns.

Quantifier Description Example
+ Helps you match any string that contains your pattern at least one time. /(ho)+/ will match hot but will not match anything without the “ho” in it.
* Similar to the previous quantifier, but the match is also valid if there are no instances of the pattern to be found. /[ho]*/ will match hot and also cold, but it’ll also match something like “cart” because this word also contains the letters “h” and “o” at least zero times.
? The * quantifier allows you to match anything repeating between zero and infinity. What if you just wanted something to repeat between zero and one time? That’s where this one comes into play. /ah?/ will match both a and ah. It’ll even match ahh (the first two letters) but not the full word.
{X}
{X,Y}
{X,}
Now imagine needing to control the amount of times a particular part of the pattern repeats. This quantifier allows you to pick either the exact amount or a range. /10{3}/ will match 1000. If you keep adding 0’s, you’ll only get a match for the first 3.

/10{3, 5}/ will actually match 1000, 10000, 100000. Adding an extra 0 will have the same effect as before.

/10{3,}/ will match any number of 0’s you add after the 3rd one. So, 1000, 10000, 10000000000, you name it.

There aren’t that many quantifiers, but they allow you to create some crazy patterns. It’s also important to note that the quantifiers can be placed next to a single character, a character class, or a capturing group (more on those later). They will also affect the way your regular expression is interpreted based on that. In other words, you can do something like this:

/10{3,}/g

Which like I showed you already, will match any number 1 followed by at least three 0’s or you can do:

This, as we’ve already shown, will match any 1 followed by at least three 0’s, or you can do this:

/[10]{3,}/g

This will match any combination of 1’s and 0’s that contains at least three numbers (i.e., 111, 000, 101, and so on). In other words, quantifiers are an amazing tool to have in your life RegExp utility belt, but they’re only as good as the rest of the expression they’re affecting.

Positional quantifiers

Before moving on to capturing groups, there are two different types of quantifiers: one that deals with amounts and another that deals with position, which allows you to match portions of a pattern based on where they are inside the string. We like to call them positional quantifiers. Let’s take a quick look.

Quantifier Description Example
$ Matching something that is at the end of your string. /Fernand[oa]\$/ matches any variation of the name, either Fernando or Fernanda but nothing else.

/is$/ will match the last is inside “it is what it is.”
^ Opposite to the previous quantifier, this one matches anything at the start of the string. /^it/ will match the starting “it” in “it is what it is.”

Conditional quantifiers

Another type of quantifier we want to cover is the one that allows you to match based on a condition. In particular, having or not having a string followed by another one. We call these conditional quantifiers:

Quantifier Description Example
XX(?= YY) A conditional match means it’ll match the string, XX, only if it’s followed by the string, YY. /is(?= what)/ will only match the first “is” in “it is what it is.”
XX(?! YY) A conditional match in which you’ll only match XX if it’s not followed by YY. /is(?! what)/ will only match the last “is” in “it is what it is.”
(?<=YY )XX A conditional match in which you’ll only match XX if it was preceded by YY. /(?<=not to )be/ will only match the last “be” in “to be or not to be.”
(?<!YY )XX Finally, a conditional match in which you’ll only match XX if it wasn’t preceded by YY. /(?<!not to )be/ will only match the first “be” in “to be or not to be.”

Notice how, for these quantifiers, we had to add them inside the parenthesis (also known as groups). If you don’t, the parser will think you’re referring to the single quantifier, ?, which has a whole different meaning.

Lookaheads and lookbehinds

A final note about these quantifiers, in particular: they have special names that you might’ve heard before.

  • The first two are called lookaheads (positive lookahead and negative lookahead, respectively) because they’re looking ahead of what you’re trying to match, i.e., looking for the condition.

  • The last two are called lookbehinds (positive lookbehind and negative lookbehind, respectively) because they’re looking back in the string for the conditional portion of the match.

Now that we’ve covered quantifiers, let’s move on to groups.

Get hands-on with 1200+ tech skills courses.