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 install --save regjsgen
API
regjsgen.generate(ast)
This function accepts an abstract syntax tree representing a regular expression, and returns the generated regular expression string.
var regjsparser = require('regjsparser');
var regjsgen = require('regjsgen');
var ast = regjsparser.parse(regex);
regex = regjsgen.generate(ast);
Support
Tested in Node.js 0.10, 0.12, 4, 6 and 8.