{"id":11924,"library":"regexp-tree","title":"RegExp Tree: Regular Expression Processor","description":"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.","status":"active","version":"0.1.27","language":"javascript","source_language":"en","source_url":"https://github.com/DmitrySoshnikov/regexp-tree","tags":["javascript","regexp","parser","AST","tree","JavaScript","ECMAScript","typescript"],"install":[{"cmd":"npm install regexp-tree","lang":"bash","label":"npm"},{"cmd":"yarn add regexp-tree","lang":"bash","label":"yarn"},{"cmd":"pnpm add regexp-tree","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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`.","wrong":"import regexpTree from 'regexp-tree';","symbol":"regexpTree","correct":"import * as regexpTree from 'regexp-tree';"},{"note":"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.","wrong":"const { parse } = require('regexp-tree');","symbol":"regexpTree","correct":"const regexpTree = require('regexp-tree');"},{"note":"When importing types like `AST` or other interface definitions, use `import type` to ensure they are correctly resolved by TypeScript and stripped during compilation.","wrong":"import { AST } from 'regexp-tree';","symbol":"AST","correct":"import type { AST } from 'regexp-tree';"}],"quickstart":{"code":"import * as regexpTree from 'regexp-tree';\n\n// Example: Parse, transform, and optimize a regular expression\nconst regexString = '/a[bc]d|efg+/gi';\n\ntry {\n  // 1. Parse the regular expression to get its AST\n  const ast = regexpTree.parse(regexString);\n  console.log('Original AST (partial):', JSON.stringify(ast, null, 2).substring(0, 100) + '...');\n\n  // 2. Transform the AST (e.g., replace a character)\n  const transformedAst = regexpTree.transform(regexString, {\n    char(path) {\n      if (path.node.value === 'b') {\n        path.node.value = 'x'; // Change 'b' to 'x'\n      }\n    }\n  });\n  console.log('Transformed Regexp:', transformedAst.toString()); // Outputs transformed regex string\n\n  // 3. Optimize the regular expression\n  const optimizedAst = regexpTree.optimize(regexString);\n  console.log('Optimized Regexp:', optimizedAst.toString());\n\n} catch (error) {\n  if (error instanceof Error) {\n    console.error('Error processing regex:', error.message);\n  } else {\n    console.error('An unknown error occurred:', error);\n  }\n}","lang":"typescript","description":"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."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"To use the CLI, install it separately via `npm install -g regexp-tree-cli` or `npm install --save-dev regexp-tree-cli`.","message":"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`.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `npm install regexp-tree` or `yarn add regexp-tree` in your project directory.","cause":"The `regexp-tree` package has not been installed or is not resolvable in the current environment.","error":"Error: Cannot find module 'regexp-tree'"},{"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(...)`.","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`.","error":"TypeError: regexpTree.parse is not a function"},{"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.","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.","error":"SyntaxError: Invalid regular expression: /pattern/: Malformed regular expression."}],"ecosystem":"npm"}