An open API service indexing awesome lists of open source software.

https://github.com/codebox/regex_parser

A regular expression parser written in JavaScript
https://github.com/codebox/regex_parser

Last synced: 7 months ago
JSON representation

A regular expression parser written in JavaScript

Awesome Lists containing this project

README

          

# regex_parser
This is a regular expression parser, written in JavaScript as a learning exercise - if you need to parse a regular expression in JavaScript you should of course use the built-in [RegExp class](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions), and not this implementation.

This library implements a backtracking [recursive descent parser](https://en.wikipedia.org/wiki/Recursive_descent_parser) and uses [this grammar](https://github.com/codebox/regex_parser/blob/master/grammar.js) to construct a parse tree from the regular expression text that you supply. The parse tree is encapsulated within a regex object, and returned by the `parse.compile()` function. The regex object exposes a `match()` method that can be used to test string values against the expression. The result of a match is contained within an object that has a `matches` property, set to either `true` or `false` to indicate whether the match succeeded or not.

var regex, match;

regex = parser.compile('abc+');
match = regex.match('abccc'); // match.matches = true
match = regex.match('abcd'); // match.matches = false

The library supports the following symbols:

Symbol

Example

* (zero or more)

abc*

+ (one or more)

abc+

? (zero or one)

abc?

. (any single character)

a.b.c

[ ] (inclusive character specification)

[A-C][a-c][123]

[^ ] (exclusive character specification)

[^A-C][^a-c][^123]

{ } (exact number of matches)

a{5}

{ , } (range of matches)

a{3,5}

{ ,} (lower bounded number of matches)

a{3,}

| (alternatives)

dog|cat|hamster

() (parentheses)

d(i|u|o)g

() \1 (capturing groups)

(1|2|3)==\1

\s and \S (whitespace/non-whitespace alias)

\S\s\S\s\S

\d and \D (digit/non-digit alias)

\d\D\d

You can try some live examples on the [project homepage](http://codebox.org.uk/pages/regex-parser)