Skip to content

Quantifiers

Mark Whitaker edited this page Aug 5, 2022 · 3 revisions

Contents

Overview

Quantifiers help you match a pattern more than once, many times, or not at all. For example:

  1. You want to find a string of consecutive digits, with at least one digit and no maximum.
  2. You want to find all the words of five letters or more within a string.

With RegexBuilder you do this by passing a RegexQuantifier object to a method. For example:

const regex = new RegexBuilder()
    .digit(RegexQuantifier.oneOrMore)
    .buildRegex();

will give us a regex that matches one or more consecutive digits, and:

const regex = new RegexBuilder()
    .letter(RegexQuantifier.atLeast(5))
    .buildRegex();

will give us a regex matching at least 5 consecutive letters.

Quantifiers can be passed into any element-matching method, or to a grouping method in which case they apply to the whole group. They are specified using properties and methods of the RegexQuantifier class.

Greedy vs lazy quantifiers

Quantifiers are said to be greedy by default: that is, they will match the longest possible string within the input. For example:

const regex = new RegexBuilder()
    .digit(RegexQuantifier.oneOrMore)
    .buildRegex();

const match = regex.exec("12345")[0];  // "12345"

If we want a quantifier to be lazy - that is, match the shortest possible string within the input - we append butAsFewAsPossible to it. For example:

const regex = new RegexBuilder()
    .digit(RegexQuantifier.oneOrMore.butAsFewAsPossible)
    .buildRegex();

const match = regex.exec("12345")[0];  // "1"

Note that the exactly() quantifier cannot be greedy or lazy, so calling butAsFewAsPossible on it will result in an error:

new RegexBuilder()
    .digit(RegexQuantifier.exactly(3).butAsFewAsPossible);    // ERROR

Properties

Quantifier Matches Raw regex equivalent
zeroOrMore Any number of occurrences of the element or group, including none at all. *
oneOrMore At least one occurrence of the element or group, but no maximum. +
noneOrOne Either zero or one occurrence of the element or group. For example, .text("http").text("s", RegexQuantifier.noneOrOne) will match both "http" and "https". ?
butAsFewAsPossible Make a greedy quantifier lazy. ? (appended to a quantifier)

Methods

Quantifier Matches Raw regex equivalent
exactly(times) Exactly the specified number of occurrences of the element or group. {x}
atLeast(minimum) At least the specified minimum number of occurrences of the element or group. {x,}
noMoreThan(maximum) No more than the specified maximum number of occurrences of the element or group. {0,x}
between(minimum, maximum) At least the specified minimum, and no more than the specified maximum, number of occurrences of the element or group. {x,y}
Clone this wiki locally