Registry / serialization / regexp-tree

regexp-tree

JSON →
library0.1.27jsnpmunverified

regexp-tree is a JavaScript library designed for comprehensive processing of regular expressions. It provides a full suite of APIs including a parser that generates an Abstract Syntax Tree (AST) based on the ECMAScript regular expression grammar, tools for AST traversal and transformation, an optimizer, and an interpreter. It also features a compatibility transpiler and supports custom regular expression extensions. Currently stable at version 0.1.27, it appears to be actively maintained, though release cadence isn't explicitly stated. Its key differentiators include its extensive set of processing capabilities beyond simple parsing, offering deep inspection, modification, and optimization of regular expressions. The library also ships with TypeScript type definitions, enhancing developer experience for TypeScript users.

npm install regexp-tree
INSTALL
IMPORT
SIG · REGEXP-TREE
R
regexp-tree
serializationjavascriptv0.1.27
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.

regexpTree
import * as regexpTree from 'regexp-tree';
import regexpTree from 'regexp-tree';
The primary export is an object containing all APIs. Use `import * as` for ESM or `require` for CommonJS to access functions like `parse`, `transform`, and `optimize`.
regexpTree
const regexpTree = require('regexp-tree');
const { parse } = require('regexp-tree');
For CommonJS, the package exports an object. Destructuring directly from `require` might not work as intended if the main module doesn't explicitly export named functions this way.
AST
import type { AST } from 'regexp-tree';
import { AST } from 'regexp-tree';
When importing types like `AST` or other interface definitions, use `import type` to ensure they are correctly resolved by TypeScript and stripped during compilation.

Demonstrates parsing a regular expression into an Abstract Syntax Tree (AST), then applying a custom transformation to modify a character, and finally optimizing the regex for improved performance or conciseness.

import * as regexpTree from 'regexp-tree'; // Example: Parse, transform, and optimize a regular expression const regexString = '/a[bc]d|efg+/gi'; try { // 1. Parse the regular expression to get its AST const ast = regexpTree.parse(regexString); console.log('Original AST (partial):', JSON.stringify(ast, null, 2).substring(0, 100) + '...'); // 2. Transform the AST (e.g., replace a character) const transformedAst = regexpTree.transform(regexString, { char(path) { if (path.node.value === 'b') { path.node.value = 'x'; // Change 'b' to 'x' } } }); console.log('Transformed Regexp:', transformedAst.toString()); // Outputs transformed regex string // 3. Optimize the regular expression const optimizedAst = regexpTree.optimize(regexString); console.log('Optimized Regexp:', optimizedAst.toString()); } catch (error) { if (error instanceof Error) { console.error('Error processing regex:', error.message); } else { console.error('An unknown error occurred:', error); } }
Debug
Known issues
breakingAs a package in `0.x.x` version, `regexp-tree` does not guarantee API stability. Minor or patch releases may introduce breaking changes without a major version bump, requiring careful review of changelogs during upgrades.
fix
Always review the package's GitHub releases or changelog when upgrading `regexp-tree` to identify any breaking changes. Consider pinning to exact versions to avoid unexpected issues.
affects: >=0.1.0
gotchaThe command-line interface (CLI) for `regexp-tree` is not included in the main `regexp-tree` package. It is distributed as a separate npm module named `regexp-tree-cli`.
fix
To use the CLI, install it separately via `npm install -g regexp-tree-cli` or `npm install --save-dev regexp-tree-cli`.
affects: >=0.1.0
gotchaWhile `regexp-tree` bases its parser on the ECMAScript regular expression grammar, it also supports 'RegExp extensions'. Using these extensions will result in regular expressions that may not be compatible with standard JavaScript RegExp engines without prior transpilation.
fix
Be mindful when using RegExp extensions; if the output regex needs to run in standard environments, ensure you utilize the `compat-transpiler` API or Babel plugins provided by `regexp-tree` to transform them into standard ECMAScript regexes.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Cannot find module 'regexp-tree'
The `regexp-tree` package has not been installed or is not resolvable in the current environment.
fix
Run `npm install regexp-tree` or `yarn add regexp-tree` in your project directory.
TypeError: regexpTree.parse is not a function
This usually occurs when attempting to destructure `parse` directly from a CommonJS `require` call, or when `import { parse } from 'regexp-tree'` is used in an environment where named ESM exports are not directly supported by the module's `package.json`.
fix
For CommonJS, use `const regexpTree = require('regexp-tree');` and then `regexpTree.parse(...)`. For ESM, use `import * as regexpTree from 'regexp-tree';` and then `regexpTree.parse(...)`.
SyntaxError: Invalid regular expression: /pattern/: Malformed regular expression.
The input string provided to `regexpTree.parse()` is not a valid regular expression according to the ECMAScript grammar or `regexp-tree`'s supported extensions.
fix
Verify the syntax of your regular expression string. Ensure all special characters are correctly escaped, character classes are properly formed, and quantifiers are used correctly. Consider testing your regex with online tools or simpler examples.
Upgrade
Version history
0.1.27latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
regexp-tree — npm install regexp-tree · libregistry