Registry / serialization / regexpp

regexpp

JSON →
library3.2.0jsnpmunverified

`regexpp` is a JavaScript library engineered for parsing and validating ECMAScript regular expressions, generating a detailed Abstract Syntax Tree (AST) that precisely reflects the regex's structure. The current stable version, 3.2.0, actively tracks and incorporates the latest updates from the ECMAScript specification, supporting features up to ES2022, such as new Unicode property escapes and the 'd' flag. Its release cadence is primarily tied to ECMAScript standard updates and Node.js LTS cycles, with minor versions typically introducing new specification features and major versions often signifying breaking changes like Node.js version deprecations or significant AST structure adjustments. Key differentiators include strict adherence to ECMAScript syntax, robust validation capabilities, and a comprehensive visitor pattern for AST traversal, making it an indispensable tool for linters, static analysis utilities, and code transformation projects.

npm install regexpp
INSTALL
IMPORT
SIG · REGEXPP
R
regexpp
serializationjavascriptv3.2.0
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.

parseRegExpLiteral
import { parseRegExpLiteral } from 'regexpp'
const { parseRegExpLiteral } = require('regexpp')
The library primarily uses ESM exports. While CommonJS `require` might work in some environments, ESM imports are the canonical and recommended approach. TypeScript types are bundled with the package.
RegExpParser
import { RegExpParser } from 'regexpp'
import RegExpParser from 'regexpp'
RegExpParser is a named export from the 'regexpp' package, not a default export. Ensure you use curly braces for destructuring.
RegExpValidator
import { RegExpValidator } from 'regexpp'
new require('regexpp').RegExpValidator()
Use the named import for the validator class to ensure type safety and proper module resolution in modern JavaScript and TypeScript environments.
AST
import type { AST } from 'regexpp'
This is a type-only import for the Abstract Syntax Tree (AST) interfaces, crucial for type-checking when working with AST nodes in TypeScript projects.

Demonstrates parsing regular expression literals and patterns, specifying ECMAScript versions, and traversing the generated AST using the visitor pattern to identify specific node types, along with basic validation.

import { parseRegExpLiteral, visitRegExpAST, RegExpValidator, RegExpParser, AST } from "regexpp"; // 1. Parse a regular expression literal and get its AST const literalSource = "/hello\s*(world)?/u"; const astLiteral = parseRegExpLiteral(literalSource); console.log("Parsed Literal (Pattern):", astLiteral.pattern.raw); console.log("Parsed Literal (Flags):", astLiteral.flags.raw); // 2. Parse a regular expression pattern directly const patternSource = "^([a-z]+)(\\d+)?$"; const parser = new RegExpParser({ ecmaVersion: 2021 }); // Specify ECMAScript version const astPattern = parser.parsePattern(patternSource, 0, patternSource.length, false); console.log("Parsed Pattern (raw):", astPattern.raw); // 3. Visit the AST to find specific nodes let capturingGroupsFound = 0; visitRegExpAST(astLiteral, { onCapturingGroupEnter(node: AST.CapturingGroup) { capturingGroupsFound++; console.log(` Found Capturing Group: ${node.name ?? '(unnamed)'} at [${node.start}, ${node.end}]`); }, onCharacterEnter(node: AST.Character) { if (node.value === 'o'.charCodeAt(0)) { console.log(` Found character 'o' at position ${node.start}`); } } }); console.log(`Total capturing groups in literal: ${capturingGroupsFound}`); // 4. Validate a regular expression (will throw on invalid input) try { new RegExpValidator().validateLiteral("/[a-z-\\s]/u"); // Valid new RegExpValidator().validateLiteral("/[b-a]/"); // Invalid range } catch (e: any) { console.error("Validation Error (expected for '[b-a]'):", e.message); }
Debug
Known issues
breakingNode.js 6.x support was dropped in v3.0.0. Users must ensure their environment runs Node.js 8 or newer to use versions 3.0.0 and above.
fix
Upgrade your Node.js runtime to version 8.x or a later LTS release.
affects: >=3.0.0
breakingThe default ECMAScript version for parsing and validation was changed to ES2020 in v3.0.0. This might cause regular expressions valid in older specifications to be flagged as errors if they rely on specific behaviors or syntax rules that changed.
fix
If your regex is valid in an older ES version (e.g., ES2019), explicitly set the `ecmaVersion` option when initializing `RegExpParser` or `RegExpValidator`: `new RegExpParser({ ecmaVersion: 2019 }).parseLiteral(...)`.
affects: >=3.0.0
breakingThe Abstract Syntax Tree (AST) shape underwent significant changes in v2.0.0. The `Disjunction` node type was removed, `Alternative` node type was added, and the `elements` property on `Pattern`, `Group`, `CapturingGroup`, and `Assertion` nodes was renamed to `alternatives`.
fix
Review and update any existing code that directly processes or traverses the AST to reflect the new node types and property names (e.g., use `alternatives` instead of `elements`).
affects: >=2.0.0 <3.0.0
gotchaSince v2.0.1, a backslash (`\`) at the very end of a regular expression pattern is disallowed to align with ECMAScript specification updates. Previously, this might have been silently accepted or led to unexpected behavior.
fix
Ensure your regular expression patterns do not end with an unescaped backslash. If a literal backslash is intended at the end, it must be escaped (e.g., `\\`).
affects: >=2.0.1
gotchaVersions 3.2.0 and later support new Unicode property escapes (ES2021) and the 'd' flag (ES2022). While `regexpp` can parse and validate these features, ensure your target JavaScript runtime environment also supports them if you intend to execute the generated regular expressions.
fix
Verify the JavaScript engine version (Node.js, browser) where your regexes will run to confirm compatibility with ES2021/ES2022 regular expression features.
affects: >=3.2.0
Errors
Common errors & fixes
Cannot find module 'regexpp' or its corresponding type declarations.
This typically occurs when using CommonJS `require()` in an ESM-only context, or if Node.js version is too old, or if TypeScript configuration is incorrect.
fix
For CommonJS, try `const { parseRegExpLiteral } = require('regexpp');`. For ESM, ensure you use `import { ... } from 'regexpp';` and that your Node.js environment is configured for ESM (e.g., Node.js >=12, or appropriate `package.json` `"type": "module"`). Ensure TypeScript `moduleResolution` is set correctly (e.g., `"node16"` or `"bundler"`).
SyntaxError: Invalid regular expression: /[b-a]/ Syntax error
The regular expression contains a syntax error according to the `ecmaVersion` specified (or the default ES2020 if not specified). This specific error indicates an invalid character range.
fix
Correct the regular expression syntax (e.g., change `[b-a]` to `[a-b]`). If the regex is valid in an older ECMAScript version, explicitly set the `ecmaVersion` option in the parser or validator options (e.g., `new RegExpParser({ ecmaVersion: 2018 })`).
TypeError: Cannot read properties of undefined (reading 'elements')
Attempting to access a deprecated AST property. The `elements` property was renamed to `alternatives` in `regexpp` v2.0.0.
fix
Update your code to use the `alternatives` property instead of `elements` when traversing `Pattern`, `Group`, `CapturingGroup`, and `Assertion` nodes in the AST.
Upgrade
Version history
3.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
regexpp — npm install regexpp · libregistry