Registry / serialization / regexp-parser-literal

regexp-parser-literal

JSON →
library1.1.40jsnpmunverified

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-literal
INSTALL
IMPORT
SIG · REGEXP-PARSER-LITE
R
regexp-parser-literal
serializationjavascriptv1.1.40
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseRegExp
import { parseRegExp } from 'regexp-parser-literal';
import parseRegExp from 'regexp-parser-literal';
This is a named export, not the default export. Primarily used to parse a full regular expression string, including slashes and flags.
astToString
import { astToString } from 'regexp-parser-literal';
const astToString = require('regexp-parser-literal').astToString;
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.
defaultRegExpParser
import { defaultRegExpParser } from 'regexp-parser-literal';
import { RegExpParser } from 'regexp-parser-literal';
`defaultRegExpParser` is an instantiated parser object from `regexpp2`, while `RegExpParser` is a type. Use this instance for methods like `parsePattern`.
EMOJI_REGEX
import { EMOJI_REGEX } from 'regexp-parser-literal';
A pre-defined RegExp object exposed by the library, useful for emoji-related pattern matching or filtering.

Demonstrates parsing a full regex literal, a pattern, converting an AST back to a string, and utilizing the `EMOJI_REGEX`.

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(', '));
Debug
Known issues
breakingMajor 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.
fix
Pin `regexp-parser-literal` and `regexpp2` to compatible versions. Test thoroughly when upgrading either package.
affects: >=1.0.0
gotchaIncorrect 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.
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.
affects: >=1.0.0
gotchaDirect 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'regexpp2'
`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).
fix
Ensure `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
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.
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(...)`.
Upgrade
Version history
1.1.40latest on npm
Audit
Dependencies
regexpp2requiredProvides the core AST parsing and manipulation engine for regular expressions.
Agent activity
4 hits · last 30 days
node
4
Resources
regexp-parser-literal — npm install regexp-parser-literal · libregistry