Regular Expression Parser for JavaScript

0.13.1 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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));

view raw JSON →