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.
npm install regexp-parser-literalVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing a full regex literal, a pattern, converting an AST back to a string, and utilizing the `EMOJI_REGEX`.
Pin `regexp-parser-literal` and `regexpp2` to compatible versions. Test thoroughly when upgrading either package.
Pass `true` for the `uFlag` parameter to `parsePattern` or configure the parser with `{ unicode: true }` in `createRegExpParser` when dealing with Unicode-aware regular expressions.Refer to the `regexpp2` documentation for the AST specification. Perform thorough testing of any modified ASTs to ensure they generate valid and intended regular expressions.
Ensure `regexpp2` is explicitly installed in your project: `npm install regexpp2` or `yarn add regexpp2`.
For 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(...)`.