Skip to content
Ese Udom edited this page Dec 15, 2023 · 6 revisions

Docs

RegExprBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.


RegExprBuilder

const builder = new RegExprBuilder();

All public methods except getRegExpr() are chain-able.

find(string)

Alias for then, sounds nicer at the beginning.

builder
    .find("Hey, ")
    .then("where do you go to get ")
    .eitherFind("juice")
    .orFind("milk");

related: then()

then(string)

Matches a single occurrence of the string.

builder
    .startOfLine()
    .then("Where do you go to get ")
    .eitherFind("juice")
    .orFind("milk");

some(array)

Specifies that there is at least one character and that each character must be one of those contained in the array of strings. Each string in the array must be a single character.

const digits = ["3", "7", "8"];
builder
    .some(digits)
    .then(".");

maybeSome(array)

The same as .some(array), except that there may be no characters at all, rather than at least one.

const digits = ["3", "7", "8"];
builder
    .maybeSome(digits);

maybe(string)

Specifies that the string may or may not occur.

builder
    .maybe("milk");

anything()

Specifies that there is zero or more of any character.

builder
    .anything();

anythingBut(string)

Specifies that there is zero or more of any character as long as the string of resulting characters does not start with the supplied string.

builder
    .anythingBut("milk");

something()

Specifies that there is one or more of any character.

builder
    .something();

lineBreak()

Specifies that there is a line break.

builder
    .lineBreak();

lineBreaks()

Specifies that there is a line break.

builder
    .exactly(2)
    .lineBreaks();

whitespace()

If used alone, specifies that there is a single whitespace character.

builder
  .whitespace();

tab()

Specifies that there is a single tab.

builder
    .tab();

tabs()

Specifies that there is a tab.

builder
    .exactly(2)
    .tabs();

digit()

Specifies that there is a single digit.

builder
    .digit();

digits()

Specifies that there is a digit.

builder
    .exactly(2)
    .digits();

letter()

Specifies that there is a single letter.

builder
    .letter();

letters()

Specifies that there is a letter.

builder
  .exactly(2)
  .letters();

lowerCaseLetter()

Specifies that there is a single lowercase letter.

builder
  .lowerCaseLetter();

lowerCaseLetters()

Specifies that there is a lowercase letter.

builder
  .exactly(2)
  .lowerCaseLetters();

upperCaseLetter()

Specifies that there is a single uppercase letter.

builder
  .upperCaseLetter();

upperCaseLetters()

Specifies that there is an uppercase letter.

builder
  .exactly(2)
  .upperCaseLetters();

startOfInput()

Matches the start of input.

builder
  .startOfInput()
  .min(1)
  .of("milk");

related: endOfInput()

startOfLine()

Matches the start of input or start of a line.

builder
  .startOfLine()
  .min(1)
  .of("milk");

related: endOfLine()

endOfInput()

Matches the end of input.

builder
  .min(1)
  .of("milk")
  .endOfInput();

related: startOfInput()

endOfLine()

Matches the end of input or line.

builder
  .min(1)
  .of("milk")
  .endOfLine();

related: startOfLine()

eitherFind(RegExprBuilder)

Used with or. This method takes a new RegExprBuilder object.

const a = builder.exactly(1).of("milk");
const b = builder.exactly(2).of("juice");

builder
  .eitherFind(a)
  .orFind(b);

related: orFind(RegExprBuilder)

orFind(RegExprBuilder)

Used after either. Multiple or methods can be chained together. This method takes a new RegExprBuilder object.

const a = builder.exactly(1).of("milk");
const b = builder.exactly(2).of("juice");

builder
  .eitherFind(a)
  .orFind(b);

related: eitherFind(RegExpBuilder)

eitherFind(string)

Used with or. This method takes a string.

builder
  .eitherFind("milk")
  .orFind("juice");

related: orFind(string)

orFind(string)

Used after either. Multiple or methods can be chained together. This method takes a string.

builder
  .eitherFind("milk")
  .orFind("juice");

related: eitherFind(string)

neither(RegExprBuilder)

Used with nor. This method takes a new RegExprBuilder object.

const a = builder.exactly(1)
  .of("milk");
const b = builder.exactly(2)
  .of("juice");

builder
  .neither(p1)
  .nor(p2);

related: nor(RegExprBuilder)

nor(RegExprBuilder)

Used after neither. Multiple nor methods can be chained together. This method takes a new RegExpBuilder object.

const a = builder.exactly(1)
  .of("milk");
const b = builder.exactly(2)
  .of("juice");

builder
  .neither(p1)
  .nor(p2);

related: neither(RegExprBuilder)

neither(string)

Used with nor. This method takes a string.

builder
  .neither("milk")
  .nor("juice");

related: nor(string)

nor(string)

Used after neither. Multiple nor methods can be chained together. This method takes a string.

builder
  .neither("milk")
  .nor("juice");

related: neither(string)

exactly(number)

Used to specify the exact number of times a pattern will repeat.

builder
  .exactly(1)
  .of("milk");

related: min(number), max(number)

min(number)

Used to specify the minimum number of times a pattern will repeat. This method can optionally occur before max()

builder
  .min(1)
  .of("milk");

related: max(number)

max(number)

Used to specify the maximum number of times a pattern will repeat. This method can optionally occur after min()

builder
  .max(1)
  .of("milk");

related: min(number)

of(string)

Used to match a string.

builder
  .exactly(1)
  .of("milk");

related: from(array), notFrom(array), like(RegExprBuilder)

anyOf(string)

Match one of multiple strings.

builder
    .startOfInput()
    .min(2).max(5).letters
    .anyOf([".jpg", ".gif", ".png"]);

related: from(array), notFrom(array))

ofAny()

Used to match any character.

builder
  .exactly(1)
  .ofAny();

related: anything()

ofGroup(number)

Used to match a previously defined group.

builder
  .exactly(3)
  .of("milk")
  .asGroup()
  .exactly(1)
  .of("juice")
  .exactly(1)
  .ofGroup(1);

related: asGroup()

from(array)

Used to match any of the characters contained in the array. It is equivalent to the regular expression [array].

builder
  .exactly(3)
  .from(["p", "q", "r"]);

related: notFrom(array)

notFrom(array)

Used to match any characters other than those contained in the array. It is equivalent to the regular expression [^array].

builder
  .exactly(3)
  .notFrom(["p", "q", "r"]);

related: from(array)

like(RegExprBuilder)

Used to nest patterns. This method takes a new RegExprBuilder object.

const pattern = builder.
  .min(1)
  .of("milk")
  .min(2)
  .of("juice");

builder
    .getNew()
    .exactly(2)
    .like(pattern);

reluctantly()

Used to specify that the pattern is matched reluctantly. This means that the smallest possible match is found, rather than the largest possible match.

builder
  .find("milk")
  .anything()
  .reluctantly()
  .digit();

ahead(RegExprBuilder)

Equivalent to the concept of look-aheads in regular expressions. This method takes a new RegExprBuilder object.

const pattern = builder
    .exactly(1)
    .of("juice");

builder
    .getNew()
    .exactly(1)
    .of("fruit")
    .ahead(pattern);

related: notAhead(RegExpBuilder)

notAhead(RegExprBuilder)

Equivalent to the concept of look-aheads in regular expressions. This method takes a new RegExprBuilder object.

const pattern = builder
    .exactly(1)
    .of("pqr");

builder
    .getNew()
    .exactly(1)
    .of("milk")
    .notAhead(pattern);

related: ahead(RegExprBuilder)

asGroup()

Equivalent to the concept of capturing groups in regular expressions.

const regExp = builder
  .min(1)
  .max(3)
  .of("p")
  .exactly(1)
  .of("milk")
  .asGroup()
  .exactly(1)
  .from(["p", "q", "r"])
  .getRegExpr();

const matches = regExpr.exec("pmilkq");
matches[1] === "milk"; //true

related: ofGroup(number)

ignoreCase()

Used before calling getRegExp() to ensure that the regular expression ignores case.

builder
  .min(1)
  .max(3)
  .of("Milk")
  .ignoreCase()
  .getRegExpr();

multiLine()

Used before calling getRegExpr() to set multiline on the regular expression. Normally when start() or end() are called, they refer to the start or end of the input. When multiLine() is called, they refer to the start or end of a line instead.

builderA
  .startOfLine()
  .min(1)
  .of("milk");

builderB
  .min(1)
  .like($builderA)
  .multiLine()
  .getRegExpr();

getRegExpr()

Once you have finished building your regular expression, getRegExpr() should be called to get the actual RegExpr object.

const regExp = builder
  .min(1)
  .of("milk")
  .multiLine()
  .getRegExpr();

regExp.matches("milk"); //true

append(RegExprBuilder)

Used to append patterns to the end of an existing pattern.

const a = builder
    .min(1)
    .of("fruit");
    
const b = builder
    .min(2)
    .of("juice");

a.append(b);

optional(RegExprBuilder)

The regular expression in the example below suggests that p1 must occur once, and then p2 might occur once.

const a = builder
    .min(1)
    .of("milk");
const b = builder
    .min(2)
    .of("juice");

a.optional(b);

RegExpr

A RegExpr Object is returned by the getRegExpr() method of the builder.

const regExpr = builder.getRegExpr();

matches(string)

Test string against current pattern

const regExp = builder
    .find("abc")
    .getRegExpr();

regExp.matches("abc"); //true

findIn(string)

Execute preg_match return matches

const regExp = builder
    .find("abc")
    .getRegExpr();

const matches = regExp.findIn("abc");

exec(string)

alias for findIn()

replace(callable)

Replace matches for current pattern

const regExp = builder
    .find("abc")
    .getRegExpr();

const matches = regExp.replace("abcdef", (match) => match.toUpperCase());

// "ABCdef"