Defining multipliers in Regex

Matchers are great but they only "scale" your pattern in one direction. I like to think of matchers as things that scale your pattern vertically, allowing you to match many more strings that fit into the same pattern, but they are still constrained in length, or scale the pattern horizontally. Multipliers allow you to match arbitrarily sized strings that you may receive as input, giving you a much greater range of freedom.

There are three basic multipliers in Regex:

  • +: This matches one or more occurrences
  • ?: This matches zero or one occurrence
  • *: This matches zero or more occurrences

We will cover these three multipliers in this section, and also show you how to create a custom multiplier.

Matching one or more occurrences

The most basic multiplier would have to be the (+) plus operator. It tells JavaScript that the pattern used in the regular expression must appear one or more times. For example, we can build upon the formatted name pattern we used before, and instead of just matching a three letter name, we could match any length of name using /[A-Z][a-z]+/g:

Matching one or more occurrences

This pattern represents anything that starts with a capital letter and has at least one lowercase letter after it. The plus sign will continue to repeat the pattern until it no longer matches (which in our case occurs when it reaches a space character).

Matching zero or one occurrence

The next multiplier, which I guess can be called more of a quantifier, is the (?) question mark. Fittingly, this multiplier allows the preceding character to either show up or not, almost as if we are saying that its presence is questionable. I think the best way to explain this is by looking at an example. Let's say we want to receive Apple in either its singular or plural form, for this, we could use this pattern:

/apples?/gi
Matching zero or one occurrence

Now this may seem like the question mark is more of a conditional operator than a multiplier, but what it is really doing is saying that the preceding character can appear either once or zero times.

Matching zero or more occurrences

The next multiplier in our tool chain is the (*) asterisk. This asterisk is a combination of the previous two multipliers, allowing the previous character to appear anywhere between zero and infinity times. So, if you have an input that contains a word or a character many times, the pattern will match. If you have an input that does not contain a word or a character, the pattern will still match. For example, this can come in handy if you are parsing some kind of log for update. In situations like this, you might get update or may update!!! and, depending on the time of day, you may even get update!!!!!!!!!!!!!!!!. To match all these strings, you can simply create the pattern /update!*/g pattern.

Matching zero or more occurrences

These are the three standard multipliers, similar to the ones that had built-in sets of characters for the (d) ranges. Similarly, Regex allows you to specify and create your own multipliers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset