regexppECMAScript 的正则表达式解析器

联合创作 · 2023-09-30 13:44

regexpp 是一个适用于 ECMAScript 的正则表达式解析器。

安装

$ npm install regexpp
  • 需要 Node.js 8 或更新版本。

用法

import {
    AST,
    RegExpParser,
    RegExpValidator,
    RegExpVisitor,
    parseRegExpLiteral,
    validateRegExpLiteral,
    visitRegExpAST
} from "regexpp"

parseRegExpLiteral

解析给定的正则表达式文字,然后生成 AST 对象。

这相当于new RegExpParser(options).parseLiteral(source).

  • 参数:
    • sourcestring | RegExp) 要解析的源代码。
    • options?RegExpParser.Options) 要解析的选项。
  • 返回:
    • 正则表达式的 AST。

validateRegExpLiteral

验证给定的正则表达式。

这相当于new RegExpValidator(options).validateLiteral(source).

访问 RegExpAST

访问给定 AST 的每个节点。

这相当于new RegExpVisitor(handlers).visit(ast).

正则解析器

新的 RegExpParser

parser.parseLiteral

解析正则表达式文字。

  • 参数:
    • sourcestring) 要解析的源代码。例如"/abc/g"
    • start?number) 源代码中的起始索引。默认为0
    • end?number) 源代码中的结束索引。默认为source.length
  • 返回:
    • 正则表达式的 AST。

parser.parsePattern

解析正则表达式模式。

  • 参数:
    • sourcestring) 要解析的源代码。例如"abc"
    • start?number) 源代码中的起始索引。默认为0
    • end?number) 源代码中的结束索引。默认为source.length
    • uFlag?boolean) 启用 Unicode 模式的标志。
  • 返回:
    • 正则表达式模式的 AST。

parser.parseFlags

解析正则表达式标志。

  • 参数:
    • sourcestring) 要解析的源代码。例如"gim"
    • start?number) 源代码中的起始索引。默认为0
    • end?number) 源代码中的结束索引。默认为source.length
  • 返回:
    • 正则表达式标志的 AST。
浏览 6
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报