Skip to content

codebox/regex_parser

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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, and not this implementation.

This library implements a backtracking recursive descent parser and uses this grammar 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

About

A regular expression parser written in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published