RegExp Tree: Regular Expression Processor
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.
Common errors
-
Error: Cannot find module 'regexp-tree'
cause The `regexp-tree` package has not been installed or is not resolvable in the current environment.fixRun `npm install regexp-tree` or `yarn add regexp-tree` in your project directory. -
TypeError: regexpTree.parse is not a function
cause 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`.fixFor 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.
cause The input string provided to `regexpTree.parse()` is not a valid regular expression according to the ECMAScript grammar or `regexp-tree`'s supported extensions.fixVerify 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.
Warnings
- breaking As 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.
- gotcha The 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`.
- gotcha While `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.
Install
-
npm install regexp-tree -
yarn add regexp-tree -
pnpm add regexp-tree
Imports
- regexpTree
import regexpTree from 'regexp-tree';
import * as regexpTree from 'regexp-tree';
- regexpTree
const { parse } = require('regexp-tree');const regexpTree = require('regexp-tree'); - AST
import { AST } from 'regexp-tree';import type { AST } from 'regexp-tree';
Quickstart
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);
}
}