Registry / serialization / regjsparser

regjsparser

JSON →
library0.13.1jsnpmunverified

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.

npm install regjsparser
INSTALL
IMPORT
SIG · REGJSPARSER
R
regjsparser
serializationjavascriptv0.13.1
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.

parse
import { parse } from 'regjsparser';
const parse = require('regjsparser').parse;
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');`.
parse (TypeScript type)
import type { AST } from 'regjsparser';
The AST type represents the structure of the parsed regular expression. Use `import type` for type-only imports to prevent bundling issues.

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.

import { parse } from 'regjsparser'; // Basic parsing of a regular expression const simplePattern = '^hello(world)?$'; const simpleAst = parse(simplePattern); console.log('Simple AST:', JSON.stringify(simpleAst, null, 2)); // Parsing with advanced features enabled via options // Note: These features are typically opt-in to maintain compatibility const advancedPattern = '(?<greeting>hi)\p{Script=Latin}(?<name>.*)(?<!bye)'; const advancedAst = parse(advancedPattern, '', { unicodePropertyEscape: true, // Enables \p{...} and \P{...} namedGroups: true, // Enables (?<name>...) lookbehind: true // Enables (?<=...) and (?<!...) }); console.log('\nAdvanced AST:', JSON.stringify(advancedAst, null, 2)); // Example of parsing a regex with flags const flaggedPattern = '/test/gi'; const flaggedAst = parse(flaggedPattern, 'gi'); console.log('\nFlagged AST:', JSON.stringify(flaggedAst, null, 2));
Debug
Known issues
gotchaSeveral 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.
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.
affects: >=0.6.0
breakingThe 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.
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).
affects: >=0.11.0
breakingBehavioral 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.
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.
affects: >=0.11.0
Errors
Common errors & fixes
SyntaxError: Invalid regular expression: /.../: Invalid group
Attempting to use named capture groups (e.g., `(?<name>...)`) without enabling the `namedGroups` option in the parser.
fix
Ensure the `namedGroups: true` option is passed to the `parse` function: `parse(pattern, flags, { namedGroups: true });`
SyntaxError: Invalid regular expression: /.../: Invalid property name
Using Unicode property escapes (e.g., `\p{Script=Latin}`) without enabling the `unicodePropertyEscape` option in the parser.
fix
Enable Unicode property escapes by passing `unicodePropertyEscape: true` to the options object: `parse(pattern, flags, { unicodePropertyEscape: true });`
TypeError: Cannot read properties of undefined (reading 'parse')
Incorrect CommonJS import for the `parse` function, often due to trying to use `require('regjsparser')` directly as a function.
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.
Upgrade
Version history
0.13.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

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