Tokenizes a string that represents a regular expression.
Tokenizes strings that represent a regular expressions.
var ret = require'ret';var tokens = ret/foo|bar/source;
tokens will contain the following object
"type": rettypesROOT"options":"type": rettypesCHAR "value" 102"type": rettypesCHAR "value" 111"type": rettypesCHAR "value" 111"type": rettypesCHAR "value" 98"type": rettypesCHAR "value" 97"type": rettypesCHAR "value" 114
ret.types is a collection of the various token types exported by ret.
Only used in the root of the regexp. This is needed due to the posibility of the root containing a pipe
| character. In that case, the token will have an
options key that will be an array of arrays of tokens. If not, it will contain a
stack key that is an array of tokens.
"type": rettypesROOT"stack": token
Groups contain tokens that are inside of a parenthesis. If the group begins with
? followed by another character, it's a special type of group. A ':' tells the group not to be remembered when
exec is used. '=' means the previous token matches only if followed by this group, and '!' means the previous token matches only if NOT followed.
Like root, it can contain an
options key instead of
stack if there is a pipe.
"type": rettypesGROUP"remember" true"followedBy": false"notFollowedBy": false"options" token
$ specify positions in the regexp.
"type": rettypesPOSITION"value": "^"
Contains a key
set specifying what tokens are allowed and a key
not specifying if the set should be negated.
"type": rettypesSET"set": token"not": false
Used in set tokens to specify a character range.
to are character codes.
"type": rettypesRANGE"from": 97"to": 122
"type": rettypesREPETITION"min": 0"max": Infinity
References a group token.
value is 1-9.
"type": rettypesREFERENCE"value": 1
Represents a single character token.
value is the character code. This might seem a bit cluttering instead of concatenating characters together. But since repetition tokens only repeat the last token and not the last clause like the pipe, it's simpler to do it this way.
"type": rettypesCHAR"value": 123
ret.js will throw errors if given a string with an invalid regular expression. All possible errors are
- Invalid group. When a group with an immediate
?character is followed by an invalid character. It can only be followed by
- Nothing to repeat. Thrown when a repetitional token is used as the first token in the current clause, as in right in the beginning of the regexp or right after a pipe. Example:
- Unmatched ). A group was not closed. Example:
- Missing ]. A custom character set was not closed. Example:
npm install ret
Tests are written with vows