Home

Andrew Jones edited this page Sep 29, 2017 · 68 revisions
Clone this wiki locally

Here you can find the API documentation for RegExpBuilder.

What is RegExpBuilder?

RegExpBuilder 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.

Javascript API

.then(string)

Matches a single occurance of the string.
var regex = r
  .startOfLine()
  .then("Where do you go to get ")
  .either("juice")
  .or("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.
var digits = ["3", "7", "8"];
var regex = r.some(digits).then(".");

.maybeSome(array)

The same as .some(array), except that there may be no characters at all, rather than at least one.
var digits = ["3", "7", "8"];
var regex = r.maybeSome(digits);

.maybe(string)

Specifies that the string may or may not occur.
var regex = r.maybe("milk");

.anything()

Specifies that there is zero or more of any character.
var regex = r.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.
var regex = r.anythingBut("milk");

.something()

Specifies that there is one or more of any character.
var regex = r.something();

.lineBreak()

Specifies that there is a line break.
var regex = r.lineBreak();

.lineBreaks()

Specifies that there is a line break.
var regex = r.exactly(2).lineBreaks();

.whitespace()

If used alone, specifies that there is a single whitespace character.
var regex = r.whitespace();

.tab()

Specifies that there is a single tab.
var regex = r.tab();

.tabs()

Specifies that there is a tab.
var regex = r.exactly(2).tabs();

.digit()

Specifies that there is a single digit.
var regex = r.digit();

.digits()

Specifies that there is a digit.
var regex = r.exactly(2).digits();

.letter()

Specifies that there is a single letter.
var regex = r.letter();

.letters()

Specifies that there is a letter.
var regex = r.exactly(2).letters();

.lowerCaseLetter()

Specifies that there is a single lowercase letter.
var regex = r.lowerCaseLetter();

.lowerCaseLetters()

Specifies that there is a lowercase letter.
var regex = r.exactly(2).lowerCaseLetters();

.upperCaseLetter()

Specifies that there is a single uppercase letter.
var regex = r.upperCaseLetter();

.upperCaseLetters()

Specifies that there is an uppercase letter.
var regex = r.exactly(2).upperCaseLetters();

.startOfInput()

Matches the start of input.
var regex = r
  .startOfInput()
  .min(1).of("milk");

related: endOfInput()

.startOfLine()

Matches the start of input or start of a line.
var regex = r
  .startOfLine()
  .min(1).of("milk");

related: endOfLine()

.endOfInput()

Matches the end of input.
var regex = r
  .min(1).of("milk")
  .endOfInput();

related: startOfInput()

.endOfLine()

Matches the end of input or line.
var regex = r
  .min(1).of("milk")
  .endOfLine();

related: startOfLine()

.either(RegExpBuilder)

Used with or. This method takes a new RegExpBuilder object.
var p1 = r.exactly(1).of("milk");
var p2 = r.exactly(2).of("juice");

var regex = r
  .either(p1)
  .or(p2);

related: or(RegExpBuilder)

.or(RegExpBuilder)

Used after either. Multiple or methods can be chained together. This method takes a new RegExpBuilder object.
var p1 = r.exactly(1).of("milk");
var p2 = r.exactly(2).of("juice");

var regex = r
  .either(p1)
  .or(p2);

related: either(RegExpBuilder)

.either(string)

Used with or. This method takes a string.
var regex = r
  .either("milk")
  .or("juice");

related: or(string)

.or(string)

Used after either. Multiple or methods can be chained together. This method takes a string.
var regex = r
  .either("milk")
  .or("juice");

related: either(string)

.neither(RegExpBuilder)

Used with nor. This method takes a new RegExpBuilder object.
var p1 = r.exactly(1).of("milk");
var p2 = r.exactly(2).of("juice");

var regex = r
  .neither(p1)
  .nor(p2);

related: nor(RegExpBuilder)

.nor(RegExpBuilder)

Used after neither. Multiple nor methods can be chained together. This method takes a new RegExpBuilder object.
var p1 = r.exactly(1).of("milk");
var p2 = r.exactly(2).of("juice");

var regex = r
  .neither(p1)
  .nor(p2);

related: neither(RegExpBuilder)

.neither(string)

Used with nor. This method takes a string.
var regex = r
  .neither("milk")
  .nor("juice");

related: nor(string)

.nor(string)

Used after neither. Multiple nor methods can be chained together. This method takes a string.
var regex = r
  .neither("milk")
  .nor("juice");

related: neither(string)

.exactly(number)

Used to specify the exact number of times a pattern will repeat.
var regex = r.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().
var regex = r.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().
var regex = r.max(1).of("milk");

related: min(number)

.of(string)

Used to match a string.
var regex = r.exactly(1).of("milk");

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

.ofAny()

Used to match any character.
var regex = r.exactly(1).ofAny();

related: anything()

.ofGroup(number)

Used to match a previously defined group.
var regex = r
  .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].
var regex = r.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].
var regex = r.exactly(3).notFrom(["p", "q", "r"]);

related: from(array)

.like(RegExpBuilder)

Used to nest patterns. This method takes a new RegExpBuilder object.
var pattern = r
  .min(1).of("milk")
  .min(2).of("juice");

var regex = r
  .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.
var regex = r
  .find("milk")
  .anything().reluctantly()
  .digit();

.ahead(RegExpBuilder)

Equivalent to the concept of lookaheads in regular expressions. This method takes a new RegExpBuilder object.
var pattern = r.exactly(1).of("juice");

var regex = r
  .exactly(1).of("fruit")
  .ahead(pattern);

related: notAhead(RegExpBuilder)

.notAhead(RegExpBuilder)

Equivalent to the concept of lookaheads in regular expressions. This method takes a new RegExpBuilder object.
var pattern = r.exactly(1).of("pqr");

var regex = r
  .exactly(1).of("milk")
  .notAhead(pattern);

related: ahead(RegExpBuilder)

.asGroup()

Equivalent to the concept of capturing groups in regular expressions.
var regex = r
  .min(1).max(3).of("p")
  .exactly(1).of("milk").asGroup()
  .exactly(1).from(["p", "q", "r"])
  .getRegExp();

regex.exec("pmilkq")[1] == "milk";

related: ofGroup(number)

.ignoreCase()

Used before calling getRegExp() to ensure that the regular expression ignores case.
var regex = r
  .min(1).max(3).of("Milk")
  .ignoreCase()
  .getRegExp();

.multiLine()

Used before calling getRegExp() 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.
var pattern = r
  .startOfLine()
  .min(1).of("milk");

var regex = r
  .min(1).like(pattern)
  .multiLine()
  .getRegExp();

.getRegExp()

Once you have finished building your regular expression, getRegExp() should be called to get the actual RegExp object.
var regex = r
  .min(1).of("milk")
  .multiLine()
  .getRegExp();

regex.test("milk");

.append(RegExpBuilder)

Used to append patterns to the end of an existing pattern.
var p1 = r.min(1).of("fruit");
var p2 = r.min(2).of("juice");

var regex = p1.append(p2);

.optional(RegExpBuilder)

The regular expression in the example below suggests that p1 must occur once, and then p2 might occur once.
var p1 = r.min(1).of("milk");
var p2 = r.min(2).of("juice");

var regex = p1.optional(p2);