Custom Tokens
Last updated
Last updated
Apart from extracting reusable expressions into variables, you can also define custom tokens directly, allowing you to use them as if they are part of the readable-regexp package for maximum convenience.
There are 3 types of custom tokens:
Constant
Tokens that modify the expression without needing parameters. These tokens are not callable.
Dynamic
Tokens that take parameters and return different expressions depending on the parameters given. These tokens must be called to provide them with parameters.
Mixed
Tokens with optional parameters. These tokens can be called or accessed directly.
Rules for custom tokens:
The token name must be a valid JavaScript identifier.
The token name must not conflict with any existing properties of RegExpToken
.
All custom tokens should be defined before any tokens are used to build regular expressions.
Defining custom tokens is a 3-step process that requires minimal effort and maintains strong typing.