{"library":"regexpp","title":"regexpp: ECMAScript Regular Expression Parser and Validator","description":"`regexpp` is a JavaScript library engineered for parsing and validating ECMAScript regular expressions, generating a detailed Abstract Syntax Tree (AST) that precisely reflects the regex's structure. The current stable version, 3.2.0, actively tracks and incorporates the latest updates from the ECMAScript specification, supporting features up to ES2022, such as new Unicode property escapes and the 'd' flag. Its release cadence is primarily tied to ECMAScript standard updates and Node.js LTS cycles, with minor versions typically introducing new specification features and major versions often signifying breaking changes like Node.js version deprecations or significant AST structure adjustments. Key differentiators include strict adherence to ECMAScript syntax, robust validation capabilities, and a comprehensive visitor pattern for AST traversal, making it an indispensable tool for linters, static analysis utilities, and code transformation projects.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install regexpp"],"cli":null},"imports":["import { parseRegExpLiteral } from 'regexpp'","import { RegExpParser } from 'regexpp'","import { RegExpValidator } from 'regexpp'","import type { AST } from 'regexpp'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parseRegExpLiteral, visitRegExpAST, RegExpValidator, RegExpParser, AST } from \"regexpp\";\n\n// 1. Parse a regular expression literal and get its AST\nconst literalSource = \"/hello\\s*(world)?/u\";\nconst astLiteral = parseRegExpLiteral(literalSource);\nconsole.log(\"Parsed Literal (Pattern):\", astLiteral.pattern.raw);\nconsole.log(\"Parsed Literal (Flags):\", astLiteral.flags.raw);\n\n// 2. Parse a regular expression pattern directly\nconst patternSource = \"^([a-z]+)(\\\\d+)?$\";\nconst parser = new RegExpParser({ ecmaVersion: 2021 }); // Specify ECMAScript version\nconst astPattern = parser.parsePattern(patternSource, 0, patternSource.length, false);\nconsole.log(\"Parsed Pattern (raw):\", astPattern.raw);\n\n// 3. Visit the AST to find specific nodes\nlet capturingGroupsFound = 0;\nvisitRegExpAST(astLiteral, {\n    onCapturingGroupEnter(node: AST.CapturingGroup) {\n        capturingGroupsFound++;\n        console.log(`  Found Capturing Group: ${node.name ?? '(unnamed)'} at [${node.start}, ${node.end}]`);\n    },\n    onCharacterEnter(node: AST.Character) {\n        if (node.value === 'o'.charCodeAt(0)) {\n            console.log(`  Found character 'o' at position ${node.start}`);\n        }\n    }\n});\nconsole.log(`Total capturing groups in literal: ${capturingGroupsFound}`);\n\n// 4. Validate a regular expression (will throw on invalid input)\ntry {\n    new RegExpValidator().validateLiteral(\"/[a-z-\\\\s]/u\"); // Valid\n    new RegExpValidator().validateLiteral(\"/[b-a]/\"); // Invalid range\n} catch (e: any) {\n    console.error(\"Validation Error (expected for '[b-a]'):\", e.message);\n}\n","lang":"typescript","description":"Demonstrates parsing regular expression literals and patterns, specifying ECMAScript versions, and traversing the generated AST using the visitor pattern to identify specific node types, along with basic validation.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}