New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

regexp-ast-analysis

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

regexp-ast-analysis

A library for analysing JS RegExp

0.7.1
latest
Source
npm
Version published
Weekly downloads
1M
8.21%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

regex

FAQs

Package last updated on 13 Sep 2023

Did you know?

Socket

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.

Install

Related posts