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')
const coordinates = oneOrMore.digit
.exactly`,`
.oneOrMore.digit
.toRegExp(Flag.Global);
console.log(coordinates.exec('[1,2] [3,4]')); // expect 2 matches
Wrap the given token in a positive lookahead.
ahead`foo`
ahead.exactly`foo`
ahead('foo')
RegExp equivalent:
/(?=foo)/
Negated token - Negative lookahead
notAhead`foo`
not.ahead`foo`
notAhead.exactly`foo`
notAhead('foo')
RegExp equivalent:
/(?!foo)/
Match a token at least the specified amount of times.
atLeast(3)`foo`
atLeast(3).exactly`foo`
atLeast(3)('foo')
RegExp equivalent:
/(?:foo){3,}/
Lazy matching
atLeastLazily(3)`foo`
atLeast.lazily(3)`foo`
RegExp equivalent:
/(?:foo){3,}?/
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.
atLeastLazily(3)`foo`
atLeast.lazily(3)`foo`
RegExp equivalent:
/(?:foo){3,}?/
Match a token at most the specified amount of times.
atMost(3)`foo`
atMost(3).exactly`foo`
atMost(3)('foo')
RegExp equivalent:
/(?:foo){0,3}/
Lazy matching
atMostLazily(3)`foo`
atMost.lazily(3)`foo`
RegExp equivalent:
/(?:foo){0,3}?/
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.
atMostLazily(3)`foo`
atMost.lazily(3)`foo`
RegExp equivalent:
/(?:foo){0,3}?/
Match a backspace character.
backspace
RegExp equivalent:
/[\b]/
Negated token
not.backspace
not(backspace)
RegExp equivalent:
/[^\b]/
Wrap the given token in a positive lookbehind.
behind`foo`
behind.exactly`foo`
behind('foo')
RegExp equivalent:
/(?<=foo)/
Negated token - Negative lookbehind
notBehind`foo`
not.behind`foo`
notBehind.exactly`foo`
notBehind('foo')
RegExp equivalent:
/(?<!foo)/
Wrap the given token in a capture group.
capture`foo`
capture.exactly`foo`
capture('foo')
RegExp equivalent:
/(foo)/
Wrap the given token in a named capture group.
captureAs`name``foo`
captureAs`name`.exactly`foo`
captureAs('name')('foo')
RegExp equivalent:
/(?<name>foo)/
Match a carriage return character.
carriageReturn
RegExp equivalent:
/\r/
Negated token
not.carriageReturn
not(carriageReturn)
RegExp equivalent:
/[^\r]/
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.
char
RegExp equivalent:
/./
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);
charIn`a-z_-`
charIn('a-z', '_-')
charIn`a-z``_-`
RegExp equivalent:
/[a-z_-]/
Negated token
notCharIn`a-z_-`
not.charIn`a-z_-`
notCharIn('a-z', '_-')
notCharIn`a-z``_-`
RegExp equivalent:
/[^a-z_-]/
Match a character ranging from the first char to the second one.
Escape sequences are supported and the two characters must be in order.
charRange`a` `z`
charRange('a', 'z')
RegExp equivalent:
/[a-z]/
Negated token
notCharRange`a` `z`
not.charRange`a` `z`
notCharRange('a', 'z')
RegExp equivalent:
/[^a-z]/
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.
control`j`
control('j')
control`J`
control('J')
RegExp equivalent:
/\cj/
/\cJ/
Negated token
not.control`j`
not.control('j')
not.control`J`
not.control('J')
RegExp equivalent:
/[^\cj]/
/[^\cJ]/
Match a character between 0 to 9.
digit
RegExp equivalent:
/\d/
Negated token
not.digit
not(digit)
RegExp equivalent:
/\D/
Match the given string literally, escaping all special characters.
exactly`match`
RegExp equivalent:
/match/
exactly`(yes/no)`
RegExp equivalent:
/\(yes/no\)/
Match a form feed character.
formFeed
RegExp equivalent:
/\f/
Negated token
not.formFeed
not(formFeed)
RegExp equivalent:
/[^\f]/
Wrap the given token in a non-capture group.
group`foo`
group.exactly`foo`
group('foo')
RegExp equivalent:
/(?:foo)/
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
.
hex`3f`
hex('3f')
RegExp equivalent:
/\x3f/
Negated token
not.hex`3f`
not(hex('3f'))
RegExp equivalent:
/[^\x3f]/
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.
oneOrMoreLazily`foo`
oneOrMore.lazily`foo`
RegExp equivalent:
/(?:foo)+?/
Assert position at the end of string, or end of line if multiline flag is set.
lineEnd
RegExp equivalent:
/$/
Negated token
not.lineEnd
not(lineEnd)
RegExp equivalent:
/(?!$)/
Match a line feed character.
lineFeed
RegExp equivalent:
/\n/
Negated token
not.lineFeed
not(lineFeed)
RegExp equivalent:
/[^\n]/
Assert position at the start of string, or start of line if multiline flag is set.
lineStart
RegExp equivalent:
/^/
Negated token
not.lineStart
not(lineStart)
RegExp equivalent:
/(?!^)/
Include another readable RegExp token in the current expression. This is useful for extracting and re-using common expressions.
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.
const filename = match(
oneOrMore.word,
exactly`_`,
oneOrMore.digit,
exactly`.txt`
);
This is equivalent to:
const filename = oneOrMore.word.exactly`_`.oneOrMore.digit.exactly`.txt`;
Match a token 0 or 1 times.
maybe`foo`
maybe.exactly`foo`
maybe('foo')
RegExp equivalent:
/(?:foo)?/
Lazy matching
maybeLazily`foo`
maybe.lazily`foo`
RegExp equivalent:
/(?:foo)??/
Match a token 0 or 1 times, trying to match as short as possible.
maybeLazily
is a short form of maybe.lazily
. See the documentation of maybe for more details.
maybeLazily`foo`
maybe.lazily`foo`
RegExp equivalent:
/(?:foo)??/
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.
not.lineFeed
not(lineFeed)
RegExp equivalent:
/[^\n]/
Wrap the given token in a negative lookahead.
notAhead
is a short form of not.ahead
. See the documentation of ahead for more details.
notAhead`foo`
not.ahead`foo`
notAhead.exactly`foo`
notAhead('foo')
RegExp equivalent:
/(?!foo)/
Wrap the given token in a negative lookbehind.
notBehind
is a short form of not.behind
. See the documentation of behind for more details.
notBehind`foo`
not.behind`foo`
notBehind.exactly`foo`
notBehind('foo')
RegExp equivalent:
/(?<!foo)/
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.
notCharIn`a-z_-`
not.charIn`a-z_-`
notCharIn('a-z', '_-')
notCharIn`a-z``_-`
RegExp equivalent:
/[^a-z_-]/
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.
notCharRange`a` `z`
not.charRange`a` `z`
notCharRange('a', 'z')
RegExp equivalent:
/[^a-z]/
Match a null character.
nullChar
RegExp equivalent:
/\0/
Negated token
not.nullChar
not(nullChar)
RegExp equivalent:
/[^\0]/
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
.
octal`123`
octal('123')
RegExp equivalent:
/[\123]/
Negated token
not.octal`123`
not(octal('123'))
RegExp equivalent:
/[^\123]/
Match one in the provided list of options.
oneOf`foo``bar`
oneOf(exactly`foo`, exactly`bar`)
oneOf('foo', 'bar')
RegExp equivalent:
/(?:foo|bar)/
Match a token 1 to infinite times.
oneOrMore`foo`
oneOrMore.exactly`foo`
oneOrMore('foo')
RegExp equivalent:
/(?:foo)+/
Lazy matching
oneOrMoreLazily`foo`
oneOrMore.lazily`foo`
RegExp equivalent:
/(?:foo)+?/
Match a token 1 to infinite times, trying to match as short as possible.
oneOrMoreLazily
is a short form of oneOrMore.lazily
. See the documentation of oneOrMore for more details.
oneOrMoreLazily`foo`
oneOrMore.lazily`foo`
RegExp equivalent:
/(?:foo)+?/
Supply a number to reference the capture group with that index. Supply a string to reference a named capture group.
Named reference
captureAs`name``foo`.ref`name`
RegExp equivalent:
/(?<name>foo)\k<name>/
Number reference
capture`foo`.ref(1)
RegExp equivalent:
/(foo)\1/
Match a token the specified amount of times. Supply 1 parameter for an exact number of times, or 2 parameters for min/max.
2 parameters for min/max
repeat(3,5)`foo`
repeat(3,5).exactly`foo`
repeat(3,5)('foo')
RegExp equivalent:
/(?:foo){3,5}/
1 parameter for exact number
repeat(3)`foo`
repeat(3).exactly`foo`
repeat(3)('foo')
RegExp equivalent:
/(?:foo){3}/
Lazy matching
repeatLazily(3,5)`foo`
repeat.lazily(3,5)`foo`
RegExp equivalent:
/(?:foo){3,5}?/
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.
repeatLazily(3,5)`foo`
repeat.lazily(3,5)`foo`
RegExp equivalent:
/(?:foo){3,5}?/
Match a tab character.
tab
RegExp equivalent:
/\t/
Negated token
not.tab
not(tab)
RegExp equivalent:
/[^\t]/
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
.
unicode`3ef1`
unicode('3ef1')
RegExp equivalent:
/\u3ef1/
Negated token
not.unicode`3ef1`
not(unicode('3ef1'))
RegExp equivalent:
/[^\u3ef1]/
Match a vertical whitespace character.
verticalWhitespace
RegExp equivalent:
/\v/
Negated token
not.verticalWhitespace
not(verticalWhitespace)
RegExp equivalent:
/[^\v]/
Match all types of whitespace characters.
When negated: match anything other than a whitespace.
whitespace
RegExp equivalent:
/\s/
Negated token
not.whitespace
not(whitespace)
RegExp equivalent:
/\S/
Match alphanumeric characters and underscore.
word
RegExp equivalent:
/\w/
Negated token
not.word
not(word)
RegExp equivalent:
/\W/
Assert position at a word boundary, between word
and non-word
characters.
wordBoundary
RegExp equivalent:
/\b/
Negated token
not.wordBoundary
not(wordBoundary)
RegExp equivalent:
/\B/
Match a token 0 to infinite times.
zeroOrMore`foo`
zeroOrMore.exactly`foo`
zeroOrMore('foo')
RegExp equivalent:
/(?:foo)*/
// !! unicode ending slash to escape comment ending !!
Lazy matching
zeroOrMoreLazily`foo`
zeroOrMore.lazily`foo`
RegExp equivalent:
/(?:foo)*?/
Match a token 0 to infinite times, trying to match as short as possible.
zeroOrMoreLazily
is a short form of zeroOrMore.lazily
. See the documentation of zeroOrMore for more details.
zeroOrMoreLazily`foo`
zeroOrMore.lazily`foo`
RegExp equivalent:
/(?:foo)*?/
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.
const coordinates = oneOrMore.digit.exactly`,`.oneOrMore.digit.toString();
expect(coordinates).toBe("\\d+,\\d+");
Generated using TypeDoc
A RegExp token that can be chained with other tokens to form a RegExp expression.