RegExp Literal AST Parser
regexp-parser-literal is a JavaScript and TypeScript library designed for parsing regular expression literals into an Abstract Syntax Tree (AST). It provides a convenient API built on top of the `regexpp2` library, offering functions such as `parseRegExp` to convert a full regular expression string into its AST representation, `parseFlags` for flag extraction, `parsePattern` for the pattern part, and `astToString` for serializing an AST back into a regular expression string. The current stable version is 1.1.40. While it does not adhere to a strict release cadence, updates are typically driven by enhancements to its underlying `regexpp2` dependency or specific utility requirements, such as improved emoji handling. A key differentiator is its ready-to-use utility functions that streamline common regex AST manipulations and its inclusion of a pre-defined `EMOJI_REGEX`. This makes it particularly useful for applications requiring programmatic analysis, validation, transformation, or generation of regular expressions, often found in linters, code formatting tools, or domain-specific language compilers. It aims to simplify direct interaction with the powerful `regexpp2` parser for common use cases.
Common errors
-
Error: Cannot find module 'regexpp2'
cause `regexp-parser-literal` relies on `regexpp2` as a direct runtime dependency, which might not be installed in some environments (e.g., if using older npm versions or specific CI setups).fixEnsure `regexpp2` is explicitly installed in your project: `npm install regexpp2` or `yarn add regexpp2`. -
TypeError: (0 , regexp_parser_literal__WEBPACK_IMPORTED_MODULE_0__.parseRegExp) is not a function
cause This error often occurs in CommonJS environments or specific bundler configurations when trying to import a named export incorrectly, or if the module resolution is misconfigured.fixFor ES Modules, ensure you're using `import { parseRegExp } from 'regexp-parser-literal';`. For CommonJS, if transpilation is not handling it correctly, try `const { parseRegExp } = require('regexp-parser-literal');` or `const parser = require('regexp-parser-literal'); const ast = parser.parseRegExp(...)`.
Warnings
- breaking Major version updates of `regexpp2`, the underlying parsing engine, may introduce breaking changes to the AST structure or parsing behavior that could affect `regexp-parser-literal`. Always review the release notes for both libraries during upgrades.
- gotcha Incorrect handling of the `u` (Unicode) flag when parsing patterns can lead to unexpected AST structures or parsing errors, especially with Unicode escape sequences or astral plane characters. Ensure the `uFlag` parameter in `parsePattern` or `createRegExpParser` options is correctly set.
- gotcha Direct modification of the AST (Abstract Syntax Tree) without deep understanding of the `regexpp2` AST specification can easily lead to invalid or malformed regular expressions when `astToString` is used. The `astToString` function does not validate the semantic correctness of the AST.
Install
-
npm install regexp-parser-literal -
yarn add regexp-parser-literal -
pnpm add regexp-parser-literal
Imports
- parseRegExp
import parseRegExp from 'regexp-parser-literal';
import { parseRegExp } from 'regexp-parser-literal'; - astToString
const astToString = require('regexp-parser-literal').astToString;import { astToString } from 'regexp-parser-literal'; - defaultRegExpParser
import { RegExpParser } from 'regexp-parser-literal';import { defaultRegExpParser } from 'regexp-parser-literal'; - EMOJI_REGEX
import { EMOJI_REGEX } from 'regexp-parser-literal';
Quickstart
import { parseRegExp, astToString, EMOJI_REGEX } from 'regexp-parser-literal';
import { defaultRegExpParser } from 'regexp-parser-literal';
// Example 1: Parsing a complete regular expression literal
const regexInput = "/hello world!/gi";
const astLiteral = parseRegExp(regexInput);
console.log('Parsed AST Type:', astLiteral.type); // RegExpLiteral
console.log('Parsed AST Body Type:', astLiteral.body.type); // Pattern
console.log('Parsed AST Flags Type:', astLiteral.flags.type); // Flags
console.log('Extracted Flags:', astLiteral.flags.raw);
// Example 2: Parsing only a pattern string using the default parser instance
const patternInput = "foo|bar\\d+";
const patternAst = defaultRegExpParser.parsePattern(patternInput);
console.log('Parsed Pattern AST Type:', patternAst.type); // Pattern
console.log('Number of pattern elements:', patternAst.elements.length);
// Example 3: Converting an AST back to a string
// In a real scenario, you would modify the AST elements before stringification
const stringifiedRegex = astToString(astLiteral);
console.log('Stringified AST back to regex:', stringifiedRegex);
// Example 4: Using the provided EMOJI_REGEX
const textWithEmoji = "Hello 👋 world! 🌍";
console.log('Does text contain emoji?', EMOJI_REGEX.test(textWithEmoji));
console.log('Matching emojis:', textWithEmoji.match(EMOJI_REGEX)?.join(', '));