What is regjsgen?
The regjsgen package is used to generate regular expressions from parsed structures. It is often used in conjunction with packages like regjsparser, which parses regular expression strings into a structured format that regjsgen can then turn back into regular expression strings. This can be useful for manipulating and generating regular expressions programmatically.
What are regjsgen's main functionalities?
Generate regular expressions from parsed objects
This feature allows you to generate a regular expression string from a parsed regular expression object. The example shows how to generate a simple literal regular expression.
const regjsgen = require('regjsgen');
const parsedRegex = {
type: 'literal',
body: 'hello',
raw: 'hello'
};
const regexString = regjsgen.generate(parsedRegex);
console.log(regexString); // Output: 'hello'
Other packages similar to regjsgen
regexp-tree
regexp-tree is a toolkit for working with regular expressions. It allows you to parse, transform, optimize, and generate regular expressions. It is more feature-rich than regjsgen, providing a full AST for regular expressions and the ability to perform complex transformations and optimizations.
regexgen
regexgen is a package for generating regular expressions that match a given set of strings. Unlike regjsgen, which generates regular expressions from parsed objects, regexgen takes a list of strings and outputs a regular expression that matches all of them. It is useful for creating compact regular expressions from a list of possible values.
regjsgen
Generate regular expressions from regjsparser’s AST.
Installation
npm i regjsgen
API
regjsgen.generate(ast)
This function accepts an abstract syntax tree representing a regular expression (see regjsparser), and returns the generated regular expression string.
const regjsparser = require('regjsparser');
const regjsgen = require('regjsgen');
let ast = regjsparser.parse(regex);
let regex = regjsgen.generate(ast);
Support
Tested on Node.js 16 and 18.
Compatible with regjsparser v0.10.0’s AST.