Interface RegExpToken

A RegExp token that can be chained with other tokens to form a RegExp expression.

Hierarchy

  • RegExpToken

Properties

toRegExp: FlagFunction

Get a RegExp object of the current expression. This is a terminal operation, which means no more functions can be chained after toRegExp, and the output cannot be converted back to a readable RegExp token.

Back-references are validated when toRegExp is called, and an error will be thrown if any of the back-references are invalid.

You can supply a list of flags to set in the RegExp object:

  • toRegExp('gmi')
    
  • toRegExp`gmi`
    
  • toRegExp(Flag.Global, Flag.MultiLine, Flag.IgnoreCase)
    
  • toRegExp('g', 'm', 'i')
    

Example

const coordinates = oneOrMore.digit
.exactly`,`
.oneOrMore.digit
.toRegExp(Flag.Global);
console.log(coordinates.exec('[1,2] [3,4]')); // expect 2 matches

Accessors

  • get atLeast(): LimitFunction & IncompleteToken
  • Match a token at least the specified amount of times.

    Returns LimitFunction & IncompleteToken

    Example

    atLeast(3)`foo`
    atLeast(3).exactly`foo`
    atLeast(3)('foo')

    RegExp equivalent:

    /(?:foo){3,}/
    

    Example

    Lazy matching

    atLeastLazily(3)`foo`
    atLeast.lazily(3)`foo`

    RegExp equivalent:

    /(?:foo){3,}?/
    
  • get atLeastLazily(): LimitFunction & IncompleteToken
  • Match a token at least the specified amount of times, trying to match as short as possible.

    atLeastLazily is a short form of atLeast.lazily. See the documentation of atLeast for more details.

    Returns LimitFunction & IncompleteToken

    Example

    atLeastLazily(3)`foo`
    atLeast.lazily(3)`foo`

    RegExp equivalent:

    /(?:foo){3,}?/
    
  • get atMost(): LimitFunction & IncompleteToken
  • Match a token at most the specified amount of times.

    Returns LimitFunction & IncompleteToken

    Example

    atMost(3)`foo`
    atMost(3).exactly`foo`
    atMost(3)('foo')

    RegExp equivalent:

    /(?:foo){0,3}/
    

    Example

    Lazy matching

    atMostLazily(3)`foo`
    atMost.lazily(3)`foo`

    RegExp equivalent:

    /(?:foo){0,3}?/
    
  • get atMostLazily(): LimitFunction & IncompleteToken
  • Match a token at most the specified amount of times, trying to match as short as possible.

    atMostLazily is a short form of atMost.lazily. See the documentation of atMost for more details.

    Returns LimitFunction & IncompleteToken

    Example

    atMostLazily(3)`foo`
    atMost.lazily(3)`foo`

    RegExp equivalent:

    /(?:foo){0,3}?/
    
  • get backspace(): RegExpToken
  • Match a backspace character.

    Returns RegExpToken

    Example

    backspace
    

    RegExp equivalent:

    /[\b]/
    

    Example

    Negated token

    not.backspace
    not(backspace)

    RegExp equivalent:

    /[^\b]/
    
  • get carriageReturn(): RegExpToken
  • Match a carriage return character.

    Returns RegExpToken

    Example

    carriageReturn
    

    RegExp equivalent:

    /\r/
    

    Example

    Negated token

    not.carriageReturn
    not(carriageReturn)

    RegExp equivalent:

    /[^\r]/
    
  • get char(): RegExpToken
  • Match any character other than newline (or including line terminators when single line flag is set).

    Note: not.char does not exist because it does not match anything.

    Returns RegExpToken

    Example

    char
    

    RegExp equivalent:

    /./
    
  • get charIn(): CharClassFunction & IncompleteToken
  • Match a character listed in the group. A hyphen denotes a range of characters, such as a-z.

    Notes:

    charIn accepts a list of strings and special sequences, but you can also combine the list into one string if you prefer:

    • charIn('a-z0-9' + whitespace)
      
    • charIn`a-z0-9${whitespace}`
      
    • charIn`a-z0-9\s`
      

    However, combining a list of options into one string is not equivalent to a simple string concatenation. - is escaped at the beginning and end of each string in the list, so charIn`a-` `z` matches a, - and z literally, while charIn`a-z` matches alphabets from a to z.

    Apart from -, ^ and ] are also escaped in the character class, so you cannot negate a charIn via a ^ character (you should use notCharIn), and you cannot close a character class prematurely.

    Backslashes \ are only escaped at the end of a string, so you can use escape sequences such as \uffff and \xff freely. If you want to include `` in the character class, you should write it at the end of a string or escape with \\.

    Additionally, charIn allows you to merge character classes by simply passing in a charIn or charRange token. For example:

    const symbols = charIn`-_*$`; // the character class to be merged must not be negated (cannot be notCharIn or notCharRange)
    const alphabet = charRange`a``z`;
    const specialWord = charIn`0-9`(alphabet)(symbols);
    const specialWord2 = charIn`0-9${alphabet}${symbols}`;
    const notSpecialWord = notCharIn`0-9`(alphabet)(symbols);

    Returns CharClassFunction & IncompleteToken

    Example

    charIn`a-z_-`
    charIn('a-z', '_-')
    charIn`a-z``_-`

    RegExp equivalent:

    /[a-z_-]/
    

    Example

    Negated token

    notCharIn`a-z_-`
    not.charIn`a-z_-`
    notCharIn('a-z', '_-')
    notCharIn`a-z``_-`

    RegExp equivalent:

    /[^a-z_-]/
    
  • get charRange(): CharRangeFunction & IncompleteToken
  • Match a character ranging from the first char to the second one.

    Escape sequences are supported and the two characters must be in order.

    Returns CharRangeFunction & IncompleteToken

    Example

    charRange`a` `z`
    charRange('a', 'z')

    RegExp equivalent:

    /[a-z]/
    

    Example

    Negated token

    notCharRange`a` `z`
    not.charRange`a` `z`
    notCharRange('a', 'z')

    RegExp equivalent:

    /[^a-z]/
    
  • get control(): ControlFunction & IncompleteToken
  • Match a control character with value equal to the given letter's character value modulo 32. Only a letter from a to z or A to Z is allowed.

    For example, \cJ represents line break (\n), because the code point of J is 74, and 74 modulo 32 is 10, which is the code point of line break. Because an uppercase letter and its lowercase form differ by 32, \cJ and \cj are equivalent. You can represent control characters from 1 to 26 in this form.

    ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape#description )

    Returns ControlFunction & IncompleteToken

    Example

    control`j`
    control('j')
    control`J`
    control('J')

    RegExp equivalent:

    /\cj/
    /\cJ/

    Example

    Negated token

    not.control`j`
    not.control('j')
    not.control`J`
    not.control('J')

    RegExp equivalent:

    /[^\cj]/
    /[^\cJ]/
  • get digit(): RegExpToken
  • Match a character between 0 to 9.

    Returns RegExpToken

    Example

    digit
    

    RegExp equivalent:

    /\d/
    

    Example

    Negated token

    not.digit
    not(digit)

    RegExp equivalent:

    /\D/
    
  • get formFeed(): RegExpToken
  • Match a form feed character.

    Returns RegExpToken

    Example

    formFeed
    

    RegExp equivalent:

    /\f/
    

    Example

    Negated token

    not.formFeed
    not(formFeed)

    RegExp equivalent:

    /[^\f]/
    
  • get hex(): LiteralFunction & IncompleteToken
  • Match a character with the given code point in base-16.

    Notes:

    Both hex and unicode have the same effect, but hex uses the single-byte escape sequence \xff if possible, while unicode always uses the 2-byte sequence \uffff.

    Returns LiteralFunction & IncompleteToken

    Example

    hex`3f`
    hex('3f')

    RegExp equivalent:

    /\x3f/
    

    Example

    Negated token

    not.hex`3f`
    not(hex('3f'))

    RegExp equivalent:

    /[^\x3f]/
    
  • get lazily(): LiteralFunction & TokenFunction & RegExpToken
  • Converts a quantifier to be lazy, causing it to match as short as possible.

    Applicable tokens: repeat, atLeast, atMost, maybe, zeroOrMore, oneOrMore

    Examples are provided in the documentation of the applicable tokens themselves.

    Returns LiteralFunction & TokenFunction & RegExpToken

    Example

    oneOrMoreLazily`foo`
    oneOrMore.lazily`foo`

    RegExp equivalent:

    /(?:foo)+?/
    
  • get lineEnd(): RegExpToken
  • Assert position at the end of string, or end of line if multiline flag is set.

    Returns RegExpToken

    Example

    lineEnd
    

    RegExp equivalent:

    /$/
    

    Example

    Negated token

    not.lineEnd
    not(lineEnd)

    RegExp equivalent:

    /(?!$)/
    
  • get lineFeed(): RegExpToken
  • Match a line feed character.

    Returns RegExpToken

    Example

    lineFeed
    

    RegExp equivalent:

    /\n/
    

    Example

    Negated token

    not.lineFeed
    not(lineFeed)

    RegExp equivalent:

    /[^\n]/
    
  • get lineStart(): RegExpToken
  • Assert position at the start of string, or start of line if multiline flag is set.

    Returns RegExpToken

    Example

    lineStart
    

    RegExp equivalent:

    /^/
    

    Example

    Negated token

    not.lineStart
    not(lineStart)

    RegExp equivalent:

    /(?!^)/
    
  • get match(): MultiTokenFunction & IncompleteToken
  • Include another readable RegExp token in the current expression. This is useful for extracting and re-using common expressions.

    Returns MultiTokenFunction & IncompleteToken

    Example

    const number = oneOrMore.digit;
    const coordinates = match(number).exactly`,`.match(number);

    This is equivalent to:

    const coordinates = oneOrMore.digit.exactly`,`.oneOrMore.digit;
    

    match can also accept multiple tokens and chain them together, which can be useful for code formatting.

    Example

    const filename = match(
    oneOrMore.word,
    exactly`_`,
    oneOrMore.digit,
    exactly`.txt`
    );

    This is equivalent to:

    const filename = oneOrMore.word.exactly`_`.oneOrMore.digit.exactly`.txt`;
    
  • get not(): TokenFunction & RegExpToken
  • Negate a given token, causing it to match anything other than the token itself.

    Negatable tokens: lineFeed, carriageReturn, backspace, tab, verticalWhitespace, formFeed, nullChar, octal, hex, unicode, word, digit, whitespace, charIn, lineStart, lineEnd, wordBoundary, ahead, behind

    Examples are provided in the documentation of the negatable tokens themselves.

    Returns TokenFunction & RegExpToken

    Example

    not.lineFeed
    not(lineFeed)

    RegExp equivalent:

    /[^\n]/
    
  • get notCharIn(): CharClassFunction & IncompleteToken
  • Match a character not listed in the group. A hyphen denotes a range of characters, such as a-z.

    notCharIn is a short form of not.charIn. For details regarding character classes, see the documentation of charIn.

    Returns CharClassFunction & IncompleteToken

    Example

    notCharIn`a-z_-`
    not.charIn`a-z_-`
    notCharIn('a-z', '_-')
    notCharIn`a-z``_-`

    RegExp equivalent:

    /[^a-z_-]/
    
  • get notCharRange(): CharRangeFunction & IncompleteToken
  • Match a character not in the range of first to second char.

    Escape sequences are supported and the two characters must be in order.

    notCharRange is a short form of not.charRange. For more details, see the documentation of charRange.

    Returns CharRangeFunction & IncompleteToken

    Example

    notCharRange`a` `z`
    not.charRange`a` `z`
    notCharRange('a', 'z')

    RegExp equivalent:

    /[^a-z]/
    
  • get nullChar(): RegExpToken
  • Match a null character.

    Returns RegExpToken

    Example

    nullChar
    

    RegExp equivalent:

    /\0/
    

    Example

    Negated token

    not.nullChar
    not(nullChar)

    RegExp equivalent:

    /[^\0]/
    
  • get octal(): LiteralFunction & IncompleteToken
  • Returns LiteralFunction & IncompleteToken

    Deprecated

    Octal escape sequences (\ followed by one, two, or three octal digits) are deprecated in string and regular expression literals.


    Match a character with the given code point in base-8.

    Notes:

    The RegExp output of octal is always wrapped in a character class to disambiguate it from capture group back-references.

    The maximum allowed value is 0o377, which is equivalent to 0xff.

    Example

    octal`123`
    octal('123')

    RegExp equivalent:

    /[\123]/
    

    Example

    Negated token

    not.octal`123`
    not(octal('123'))

    RegExp equivalent:

    /[^\123]/
    
  • get repeat(): RepeatFunction & IncompleteToken
  • Match a token the specified amount of times. Supply 1 parameter for an exact number of times, or 2 parameters for min/max.

    Returns RepeatFunction & IncompleteToken

    Example

    2 parameters for min/max

    repeat(3,5)`foo`
    repeat(3,5).exactly`foo`
    repeat(3,5)('foo')

    RegExp equivalent:

    /(?:foo){3,5}/
    

    Example

    1 parameter for exact number

    repeat(3)`foo`
    repeat(3).exactly`foo`
    repeat(3)('foo')

    RegExp equivalent:

    /(?:foo){3}/
    

    Example

    Lazy matching

    repeatLazily(3,5)`foo`
    repeat.lazily(3,5)`foo`

    RegExp equivalent:

    /(?:foo){3,5}?/
    
  • get repeatLazily(): RepeatFunction & IncompleteToken
  • Match a token the specified amount of times, trying to match as short as possible. Supply 1 parameter for an exact number of times, or 2 parameters for min/max.

    repeatLazily is a short form of repeat.lazily. See the documentation of repeat for more details.

    Returns RepeatFunction & IncompleteToken

    Example

    repeatLazily(3,5)`foo`
    repeat.lazily(3,5)`foo`

    RegExp equivalent:

    /(?:foo){3,5}?/
    
  • get tab(): RegExpToken
  • Match a tab character.

    Returns RegExpToken

    Example

    tab
    

    RegExp equivalent:

    /\t/
    

    Example

    Negated token

    not.tab
    not(tab)

    RegExp equivalent:

    /[^\t]/
    
  • get unicode(): LiteralFunction & IncompleteToken
  • Match a character with the given code point in base-16.

    Notes:

    Both hex and unicode have the same effect, but hex uses the single-byte escape sequence \xff if possible, while unicode always uses the 2-byte sequence \uffff.

    Returns LiteralFunction & IncompleteToken

    Example

    unicode`3ef1`
    unicode('3ef1')

    RegExp equivalent:

    /\u3ef1/
    

    Example

    Negated token

    not.unicode`3ef1`
    not(unicode('3ef1'))

    RegExp equivalent:

    /[^\u3ef1]/
    
  • get verticalWhitespace(): RegExpToken
  • Match a vertical whitespace character.

    Returns RegExpToken

    Example

    verticalWhitespace
    

    RegExp equivalent:

    /\v/
    

    Example

    Negated token

    not.verticalWhitespace
    not(verticalWhitespace)

    RegExp equivalent:

    /[^\v]/
    
  • get whitespace(): RegExpToken
  • Match all types of whitespace characters.

    When negated: match anything other than a whitespace.

    Returns RegExpToken

    Example

    whitespace
    

    RegExp equivalent:

    /\s/
    

    Example

    Negated token

    not.whitespace
    not(whitespace)

    RegExp equivalent:

    /\S/
    
  • get word(): RegExpToken
  • Match alphanumeric characters and underscore.

    Returns RegExpToken

    Example

    word
    

    RegExp equivalent:

    /\w/
    

    Example

    Negated token

    not.word
    not(word)

    RegExp equivalent:

    /\W/
    
  • get wordBoundary(): RegExpToken
  • Assert position at a word boundary, between word and non-word characters.

    Returns RegExpToken

    Example

    wordBoundary
    

    RegExp equivalent:

    /\b/
    

    Example

    Negated token

    not.wordBoundary
    not(wordBoundary)

    RegExp equivalent:

    /\B/
    

Methods

  • Get the current expression as a RegExp string. This is a terminal operation, which means no more functions can be chained after toString, and the string output cannot be converted back to a readable RegExp token.

    Back-references are validated when toString is called, and an error will be thrown if any of the back-references are invalid.

    Returns string

    Example

    const coordinates = oneOrMore.digit.exactly`,`.oneOrMore.digit.toString();
    expect(coordinates).toBe("\\d+,\\d+");

Generated using TypeDoc