Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
regjsparser
Advanced tools
The regjsparser npm package is a JavaScript library that parses regular expressions into an abstract syntax tree (AST). This can be useful for analyzing, transforming, or generating regular expressions programmatically.
Parsing Regular Expressions
This feature allows you to parse a regular expression into an abstract syntax tree (AST). The AST can then be used for further analysis or transformation.
const regjsparser = require('regjsparser');
const regex = /abc/;
const ast = regjsparser.parse(regex);
console.log(JSON.stringify(ast, null, 2));
Handling Unicode
This feature allows you to parse regular expressions that include Unicode characters. The 'u' flag is used to indicate that the regular expression should be treated as a sequence of Unicode code points.
const regjsparser = require('regjsparser');
const regex = /\u{1F600}/u;
const ast = regjsparser.parse(regex);
console.log(JSON.stringify(ast, null, 2));
Error Handling
This feature provides error handling capabilities when parsing invalid regular expressions. It throws an error with a descriptive message if the regular expression is not valid.
const regjsparser = require('regjsparser');
try {
const regex = /[a-z/;
const ast = regjsparser.parse(regex);
} catch (e) {
console.error('Parsing error:', e.message);
}
regexpp is a regular expression parser for ECMAScript. It parses regular expressions into an AST and provides utilities for traversing and analyzing the AST. Compared to regjsparser, regexpp offers more detailed error messages and supports the latest ECMAScript features.
regexp-tree is a regular expression processing library that includes a parser, transformer, and optimizer. It parses regular expressions into an AST and provides tools for transforming and optimizing them. Unlike regjsparser, regexp-tree focuses on providing a complete suite of tools for working with regular expressions.
regex-parser is a simple library for parsing regular expressions into an AST. It is less feature-rich compared to regjsparser and focuses on providing a minimalistic approach to parsing regular expressions.
Parsing the JavaScript's RegExp in JavaScript.
npm install regjsparser
var parse = require('regjsparser').parse;
var parseTree = parse('^a'); // /^a/
console.log(parseTree);
// Toggle on/off additional features:
var parseTree = parse('^a', '', {
// SEE: https://github.com/jviereck/regjsparser/pull/78
unicodePropertyEscape: true,
// SEE: https://github.com/jviereck/regjsparser/pull/83
namedGroups: true,
// SEE: https://github.com/jviereck/regjsparser/pull/89
lookbehind: true
});
console.log(parseTree);
To run the tests, run the following command:
npm test
To create a new reference file, execute…
node test/update-fixtures.js
…from the repo top directory.
FAQs
Parsing the JavaScript's RegExp in JavaScript.
The npm package regjsparser receives a total of 25,347,872 weekly downloads. As such, regjsparser popularity was classified as popular.
We found that regjsparser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.