Getting Started
The package has two main functions. parseRegex parses patterns, and parseSubstitution parses
substitution strings.
Requirements
- Node.js 18+
- An active commercial subscription
- npm, pnpm, or yarn
Install
Install the package from npm:
npm install @r101/parser
Core API
Parse a pattern with a flavor and its active flags:
import { Flavors, parseRegex } from '@r101/parser';
const pattern = '^(?<name>[a-z]+)-(?<id>\\d+)$';
const regexResult = parseRegex(pattern, {
flavor: Flavors.PCRE2,
flags: 'm',
});
The flags option is required. For a pattern with no flags, use an empty string.
Parse a substitution string with the same flavor:
import { Flavors, parseSubstitution } from '@r101/parser';
const substitutionResult = parseSubstitution('$&', {
flavor: Flavors.PCRE2,
flags: '',
});
parseSubstitution parses and validates the substitution syntax. It does not apply the
substitution to matched text.
Parse result
Both functions return the same result shape:
type ParseResult<T> = {
tokens: T[];
state: ParserState;
patternError: boolean;
};
tokens remains available after a syntax error. Each invalid token contains a typed error code and
its source position.
Options
flavorselects one of the values inFlavors.flagssets the active regex flags.delimitersets an optional pattern or substitution delimiter.invalidFlagsBehaviorselectsthroworsanitize. The default isthrow.featureFlagschanges supported optional syntax for a flavor.overrideStatesupplies regex state to a related parse, such as capture data for a substitution.decomposeQuotedLiteralsexposes the atoms inside\Q...\Efor analysis tools.
Runtime compatibility
- Browser: supported
- Node.js: supported
- Module formats: ESM and CommonJS
- Tree shaking: supported
Next steps
- Read Capabilities for syntax and output details.
- Read Performance and Reliability for performance and test information.