Skip to main content
The Deno 2 Release Candidate is here
Learn more

RegexParse

Regex-parse is a simple regular expression parser that parses a string of regular expression and outpus a json tree.

Table of Contents

Usage from Deno

Parsing a string:

import { parse } from "https://deno.land/x/regex_parse/mod.ts"

console.log(JSON.stringify(parse("(?<group>hi34f){3,4}|$\n")));

Sample output:

{
  "type": "union",
  "left": {
    "type": "quntifier",
    "kind": "range",
    "from": 3,
    "to": 4,
    "child": {
      "type": "group",
      "kind": "named",
      "child": {
        "type": "concat",
        "left": {
          "type": "concat",
          "left": {
            "type": "concat",
            "left": {
              "type": "concat",
              "left": {
                "type": "char",
                "value": "h"
              },
              "right": {
                "type": "char",
                "value": "i"
              }
            },
            "right": {
              "type": "char",
              "value": "3"
            }
          },
          "right": {
            "type": "char",
            "value": "4"
          }
        },
        "right": {
          "type": "char",
          "value": "f"
        }
      },
      "name": "group"
    }
  },
  "right": {
    "type": "concat",
    "left": {
      "type": "anchor",
      "value": "$"
    },
    "right": {
      "type": "control",
      "value": "\n"
    }
  }
}

Api

Node

Any object that has a type attribute

interface Node {
  type: string;
}

Here are the interfaces to each kind of node that you can access:

Char

A simple literal character

interface Char {
  type: "char";
  value: string;
}

Anchor

Anchors $ or ^

interface Anchor {
  type: "anchor";
  value: "$" | "^";
}

Control

A control character

interface Control {
    type: "Control";
    value: string;
}

Escaped

A character escaped with /

interface Escaped {
  type: "escaped";
  value: string;
}

Null

This node represents nothing or the empty string

interface Null {
  type: "null";
}

CharacterClass

A character class within []

interface CharacterClass {
  type: "characterClass";
  negated: boolean;
  expressions: (Char | ClassRange)[];
}

ClassRange

Class range in the format of x-y in which the codepoint of x has to be smaller than y

interface ClassRange {
  type: "classRange";
  from: string;
  to: string;
}

Star

The kleene star

interface Star {
  type: "quntifier";
  kind: "star"
  child: Node;
}

Plus

The kleene plus

interface Plus {
  type: "quntifier";
  kind: "plus";
  child: Node;
}

Range

A range quntifier of the form x{min,max}

interface Range {
  type: "quntifier";
  kind: "string";
  from: number;
  to?: number;
}

Simple

An ordinar capturing group of the form (…)

interface Simple {
  type: "group";
  kind: "simple"
  child: Node;
}

LookAhead

Look ahead in the form (?=…) or negated (?!…)

interface LookAhead {
  type: "group";
  kind: "lookahead";
  negated: boolean;
  child: Node;
}

LookBehind

Look behind in the form (?<=…) or negated (?<!…)

interface LookBehind {
  type: "group";
  kind: "lookahead";
  negated: boolean;
  child: Node;
}

NonCapturing

Noncapturing group in the form (?:…)

interface NonCapturing {
  type: "group";
  kind: "nonCapturing";
  child: Node;
}

Named

A named group of the form (?<name>…)

interface Named {
  type: "group";
  kind: "named";
  child: Node;
}

User defined type guard

This module has built in type guard to help you check the type of a node. There is one for every type of node

example:

import { parse , guard, Node } from "./mod.ts";
let result = parse("(hi)+");

function walk(s: Node) {
  if (guard.isStar(s)) {
    ...
  }

  if (guard.isNamed(s)) {
    ...
  }
}