{"id":15801,"library":"regexp-parser-literal","title":"RegExp Literal AST Parser","description":"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.","status":"active","version":"1.1.40","language":"javascript","source_language":"en","source_url":"https://github.com/bluelovers/ws-regexp","tags":["javascript","build","emoji","literal","parser","re","ast","regex","regexp","typescript"],"install":[{"cmd":"npm install regexp-parser-literal","lang":"bash","label":"npm"},{"cmd":"yarn add regexp-parser-literal","lang":"bash","label":"yarn"},{"cmd":"pnpm add regexp-parser-literal","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the core AST parsing and manipulation engine for regular expressions.","package":"regexpp2","optional":false}],"imports":[{"note":"This is a named export, not the default export. Primarily used to parse a full regular expression string, including slashes and flags.","wrong":"import parseRegExp from 'regexp-parser-literal';","symbol":"parseRegExp","correct":"import { parseRegExp } from 'regexp-parser-literal';"},{"note":"CommonJS users must destructure named exports or access them as properties of the main export object. Essential for converting an AST back into a regular expression string.","wrong":"const astToString = require('regexp-parser-literal').astToString;","symbol":"astToString","correct":"import { astToString } from 'regexp-parser-literal';"},{"note":"`defaultRegExpParser` is an instantiated parser object from `regexpp2`, while `RegExpParser` is a type. Use this instance for methods like `parsePattern`.","wrong":"import { RegExpParser } from 'regexp-parser-literal';","symbol":"defaultRegExpParser","correct":"import { defaultRegExpParser } from 'regexp-parser-literal';"},{"note":"A pre-defined RegExp object exposed by the library, useful for emoji-related pattern matching or filtering.","symbol":"EMOJI_REGEX","correct":"import { EMOJI_REGEX } from 'regexp-parser-literal';"}],"quickstart":{"code":"import { parseRegExp, astToString, EMOJI_REGEX } from 'regexp-parser-literal';\nimport { defaultRegExpParser } from 'regexp-parser-literal';\n\n// Example 1: Parsing a complete regular expression literal\nconst regexInput = \"/hello world!/gi\";\nconst astLiteral = parseRegExp(regexInput);\nconsole.log('Parsed AST Type:', astLiteral.type); // RegExpLiteral\nconsole.log('Parsed AST Body Type:', astLiteral.body.type); // Pattern\nconsole.log('Parsed AST Flags Type:', astLiteral.flags.type); // Flags\nconsole.log('Extracted Flags:', astLiteral.flags.raw);\n\n// Example 2: Parsing only a pattern string using the default parser instance\nconst patternInput = \"foo|bar\\\\d+\";\nconst patternAst = defaultRegExpParser.parsePattern(patternInput);\nconsole.log('Parsed Pattern AST Type:', patternAst.type); // Pattern\nconsole.log('Number of pattern elements:', patternAst.elements.length);\n\n// Example 3: Converting an AST back to a string\n// In a real scenario, you would modify the AST elements before stringification\nconst stringifiedRegex = astToString(astLiteral);\nconsole.log('Stringified AST back to regex:', stringifiedRegex);\n\n// Example 4: Using the provided EMOJI_REGEX\nconst textWithEmoji = \"Hello 👋 world! 🌍\";\nconsole.log('Does text contain emoji?', EMOJI_REGEX.test(textWithEmoji));\nconsole.log('Matching emojis:', textWithEmoji.match(EMOJI_REGEX)?.join(', '));\n","lang":"typescript","description":"Demonstrates parsing a full regex literal, a pattern, converting an AST back to a string, and utilizing the `EMOJI_REGEX`."},"warnings":[{"fix":"Pin `regexp-parser-literal` and `regexpp2` to compatible versions. Test thoroughly when upgrading either package.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Pass `true` for the `uFlag` parameter to `parsePattern` or configure the parser with `{ unicode: true }` in `createRegExpParser` when dealing with Unicode-aware regular expressions.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure `regexpp2` is explicitly installed in your project: `npm install regexpp2` or `yarn add 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).","error":"Error: Cannot find module 'regexpp2'"},{"fix":"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(...)`.","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.","error":"TypeError: (0 , regexp_parser_literal__WEBPACK_IMPORTED_MODULE_0__.parseRegExp) is not a function"}],"ecosystem":"npm"}