readable-regexp
TypeDocGitHubnpm
  • readable-regexp
  • Getting Started
    • Installation
    • Usage
  • API Reference
    • Character Classes
    • Anchors
    • Special Tokens
    • Quantifiers
    • Groups
    • Extras
    • TypeDoc
  • Custom Extensions
    • Custom Tokens
      • Step 1: Type Definition
      • Step 2: Implementation
      • Step 3: Usage
Powered by GitBook
On this page
  • match
  • toString()
  • toRegExp(flags?)
  1. API Reference

Extras

match

Include another readable RegExp token in the current expression. This is useful for extracting and re-using common expressions.

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`;

toString()

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.

Example:

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

toRegExp(flags?)

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
PreviousGroupsNextCustom Tokens

Last updated 1 year ago