Codebox Software
JavaScript Regular Expression Parser
Published:
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 (try changing the values in the green text boxes):
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 your own expression here:
Regular Expression: | |
Text: |