What is regexp-to-ast?
The `regexp-to-ast` npm package is a utility that converts regular expressions into Abstract Syntax Trees (ASTs). This can be useful for analyzing, transforming, or generating regular expressions programmatically.
What are regexp-to-ast's main functionalities?
Convert RegExp to AST
This feature allows you to convert a regular expression into its corresponding Abstract Syntax Tree (AST). The AST can then be used for further analysis or transformation.
const { regExpParser } = require('regexp-to-ast');
const ast = regExpParser.pattern('/a*b+/');
console.log(JSON.stringify(ast, null, 2));
Analyze AST
This feature demonstrates how to traverse and analyze the AST generated from a regular expression. You can inspect different nodes and their properties.
const { regExpParser } = require('regexp-to-ast');
const ast = regExpParser.pattern('/a*b+/');
function analyzeAST(node) {
if (node.type === 'Character') {
console.log(`Character: ${node.value}`);
}
if (node.children) {
node.children.forEach(analyzeAST);
}
}
analyzeAST(ast);
Other packages similar to regexp-to-ast
regexpp
The `regexpp` package is a regular expression parser for ECMAScript. It provides a way to parse regular expressions into ASTs and offers utilities for AST traversal and manipulation. Compared to `regexp-to-ast`, `regexpp` supports a broader range of ECMAScript regular expression features and provides more detailed AST nodes.
regexp-tree
The `regexp-tree` package is a toolkit for working with regular expressions. It includes a parser that converts regular expressions into ASTs, as well as utilities for transforming and generating regular expressions. `regexp-tree` offers a more comprehensive set of tools for working with regular expressions compared to `regexp-to-ast`.
regexp-to-ast
Reads a JavaScript Regular Expression literal(text) and outputs an Abstract Syntax Tree.
Installation
API
The API is defined as a TypeScript definition file.
Usage
-
Parsing to an AST:
const RegExpParser = require("regexp-to-ast").RegExpParser
const regexpParser = new RegExpParser.parser()
const astOutput = regexpParser.pattern("/a|b|c/g")
const input2 = /a|b/.toString()
const anotherAstOutput = regexpParser.pattern(input2)
-
Visiting the AST:
const { RegExpParser, BaseRegExpVisitor } = require("regexp-to-ast")
const regexpParser = new RegExpParser.parser()
const regExpAst = regexpParser.pattern("/a|b|c/g")
class MyRegExpVisitor extends BaseRegExpVisitor {
visitPattern(node) {}
visitFlags(node) {}
visitDisjunction(node) {}
visitAlternative(node) {}
visitStartAnchor(node) {}
visitEndAnchor(node) {}
visitWordBoundary(node) {}
visitNonWordBoundary(node) {}
visitLookahead(node) {}
visitNegativeLookahead(node) {}
visitCharacter(node) {}
visitSet(node) {}
visitGroup(node) {}
visitGroupBackReference(node) {}
visitQuantifier(node) {}
}
const myVisitor = new MyRegExpVisitor()
myVisitor.visit(regExpAst)
Compatibility
This library is written in ES5 style and is compatiable with all major browsers and modern node.js versions.
TODO / Limitations