What is regexp-ast-analysis?
The 'regexp-ast-analysis' npm package provides tools for analyzing and manipulating the abstract syntax tree (AST) of regular expressions. It allows developers to parse regular expressions into an AST, traverse and modify the AST, and perform various analyses on the structure of the regular expression.
What are regexp-ast-analysis's main functionalities?
Parsing Regular Expressions
This feature allows you to parse a regular expression into its corresponding AST. The code sample demonstrates how to parse the regular expression '/abc/' and output its AST structure.
const { parseRegExpLiteral } = require('regexp-ast-analysis');
const ast = parseRegExpLiteral('/abc/');
console.log(JSON.stringify(ast, null, 2));
Traversing the AST
This feature allows you to traverse the AST of a regular expression. The code sample demonstrates how to visit each character node in the AST and log its raw value.
const { parseRegExpLiteral, visitRegExpAST } = require('regexp-ast-analysis');
const ast = parseRegExpLiteral('/abc/');
visitRegExpAST(ast, {
onCharacterEnter(node) {
console.log('Character:', node.raw);
}
});
Modifying the AST
This feature allows you to modify the AST of a regular expression. The code sample demonstrates how to change the character 'a' to 'x' in the AST.
const { parseRegExpLiteral, visitRegExpAST } = require('regexp-ast-analysis');
const ast = parseRegExpLiteral('/abc/');
visitRegExpAST(ast, {
onCharacterEnter(node) {
if (node.raw === 'a') {
node.raw = 'x';
}
}
});
console.log(JSON.stringify(ast, null, 2));
Analyzing the AST
This feature allows you to perform various analyses on the AST of a regular expression. The code sample demonstrates how to analyze the AST of the regular expression '/a|b/' and output the analysis results.
const { parseRegExpLiteral, analyzeRegExpAST } = require('regexp-ast-analysis');
const ast = parseRegExpLiteral('/a|b/');
const analysis = analyzeRegExpAST(ast);
console.log(analysis);
Other packages similar to regexp-ast-analysis
regexpp
The 'regexpp' package is a regular expression parser and AST utility. It provides similar functionality to 'regexp-ast-analysis' in terms of parsing regular expressions into an AST and traversing the AST. However, 'regexpp' focuses more on providing a detailed and spec-compliant AST structure.
regexp-tree
The 'regexp-tree' package offers a toolkit for working with regular expressions, including parsing, transforming, and optimizing them. It provides similar AST manipulation capabilities as 'regexp-ast-analysis' but also includes additional features like regular expression optimization and code generation.
regex-parser
The 'regex-parser' package is a simple utility for parsing regular expressions into an AST. While it provides basic parsing functionality similar to 'regexp-ast-analysis', it lacks the advanced analysis and traversal features found in 'regexp-ast-analysis'.
RegExp AST analysis
This is a library providing functionalities to analyse JavaScript RegExp.
All functions operate on AST nodes produced by regexpp. Characters are parsed by refa.
Usage
Install the library using npm:
npm i regexp-ast-analysis
Import the library:
import * as RAA from "regexp-ast-analysis";
Documentation
Links to the full API documentation: