{"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install regexp-parser-literal"],"cli":null},"imports":["import { parseRegExp } from 'regexp-parser-literal';","import { astToString } from 'regexp-parser-literal';","import { defaultRegExpParser } from 'regexp-parser-literal';","import { EMOJI_REGEX } from 'regexp-parser-literal';"],"auth":{"required":false,"env_vars":[]},"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`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}