{"id":11925,"library":"regjsparser","title":"Regular Expression Parser for JavaScript","description":"RegJSParser is a JavaScript library designed for parsing JavaScript regular expressions into an abstract syntax tree (AST). It provides a programmatic way to analyze and manipulate regular expression patterns, enabling tools that need to understand the structure of regular expressions. The current stable version is 0.13.1, with recent releases indicating an active maintenance schedule, primarily focused on dependency updates, performance improvements, bug fixes, and keeping up with the latest Unicode specifications (e.g., Unicode 17.0.0 in v0.13.0). Key differentiators include its ability to parse various modern RegExp features, such as Unicode property escapes, named capture groups, and lookbehind assertions, which are often toggled via an options object during parsing. It ships with TypeScript types, facilitating its integration into modern TypeScript projects.","status":"active","version":"0.13.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/jviereck/regjsparser","tags":["javascript","typescript"],"install":[{"cmd":"npm install regjsparser","lang":"bash","label":"npm"},{"cmd":"yarn add regjsparser","lang":"bash","label":"yarn"},{"cmd":"pnpm add regjsparser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While the library supports CommonJS, ESM named imports are the modern best practice, especially with TypeScript. The CommonJS pattern `require('regjsparser').parse` is also valid for older environments but can be refactored to a destructured import like `const { parse } = require('regjsparser');`.","wrong":"const parse = require('regjsparser').parse;","symbol":"parse","correct":"import { parse } from 'regjsparser';"},{"note":"The AST type represents the structure of the parsed regular expression. Use `import type` for type-only imports to prevent bundling issues.","symbol":"parse (TypeScript type)","correct":"import type { AST } from 'regjsparser';"}],"quickstart":{"code":"import { parse } from 'regjsparser';\n\n// Basic parsing of a regular expression\nconst simplePattern = '^hello(world)?$';\nconst simpleAst = parse(simplePattern);\nconsole.log('Simple AST:', JSON.stringify(simpleAst, null, 2));\n\n// Parsing with advanced features enabled via options\n// Note: These features are typically opt-in to maintain compatibility\nconst advancedPattern = '(?<greeting>hi)\\p{Script=Latin}(?<name>.*)(?<!bye)';\nconst advancedAst = parse(advancedPattern, '', {\n  unicodePropertyEscape: true, // Enables \\p{...} and \\P{...}\n  namedGroups: true,           // Enables (?<name>...)\n  lookbehind: true             // Enables (?<=...) and (?<!...)\n});\nconsole.log('\\nAdvanced AST:', JSON.stringify(advancedAst, null, 2));\n\n// Example of parsing a regex with flags\nconst flaggedPattern = '/test/gi';\nconst flaggedAst = parse(flaggedPattern, 'gi');\nconsole.log('\\nFlagged AST:', JSON.stringify(flaggedAst, null, 2));","lang":"typescript","description":"Demonstrates basic and advanced parsing of regular expressions, including enabling opt-in features like Unicode property escapes, named groups, and lookbehind assertions, and parsing with flags."},"warnings":[{"fix":"Always pass an options object to `parse(pattern, flags, { unicodePropertyEscape: true, namedGroups: true, lookbehind: true })` if you intend to use these features. Refer to the documentation for specific version compatibility of each option.","message":"Several advanced RegExp features like Unicode property escapes (\\p{...}), named capture groups ((?<name>...)), and lookbehind assertions ((?<=...)) are not enabled by default. They must be explicitly turned on via the `options` object passed to the `parse` function.","severity":"gotcha","affected_versions":">=0.6.0"},{"fix":"Review your regular expressions, especially those using Unicode property escapes (e.g., `\\p{Script=...}`), after upgrading major or minor versions that include Unicode updates (e.g., v0.11.0 to Unicode 15.1.0/16.0.0, v0.13.0 to Unicode 17.0.0).","message":"The library frequently updates its support for Unicode versions to align with the latest ECMAScript specification. Changes in Unicode data (e.g., property values, character ranges) can subtly alter parsing results for Unicode-aware regular expressions.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Thoroughly test existing regular expressions after upgrading to `v0.11.0` or later, particularly those using anchors (`^`, `$`) with quantifiers or intricate lookbehind constructs in Unicode mode. Consult the changelog for details on specific affected patterns.","message":"Behavioral changes were introduced in `v0.11.0` regarding quantifiable anchors in Unicode mode and modifiers in lookbehind assertions. This could lead to different parsing outcomes or errors for certain complex regular expression patterns that previously parsed successfully.","severity":"breaking","affected_versions":">=0.11.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the `namedGroups: true` option is passed to the `parse` function: `parse(pattern, flags, { namedGroups: true });`","cause":"Attempting to use named capture groups (e.g., `(?<name>...)`) without enabling the `namedGroups` option in the parser.","error":"SyntaxError: Invalid regular expression: /.../: Invalid group"},{"fix":"Enable Unicode property escapes by passing `unicodePropertyEscape: true` to the options object: `parse(pattern, flags, { unicodePropertyEscape: true });`","cause":"Using Unicode property escapes (e.g., `\\p{Script=Latin}`) without enabling the `unicodePropertyEscape` option in the parser.","error":"SyntaxError: Invalid regular expression: /.../: Invalid property name"},{"fix":"For CommonJS, use `const { parse } = require('regjsparser');` or `const parse = require('regjsparser').parse;`. Ensure you are accessing the `parse` property from the module export.","cause":"Incorrect CommonJS import for the `parse` function, often due to trying to use `require('regjsparser')` directly as a function.","error":"TypeError: Cannot read properties of undefined (reading 'parse')"}],"ecosystem":"npm"}