(protected) new Term(body, quantifiers)
Construct a Term object.
The Term object represents a valid fragment of regular expression that forms a small part of the whole regular expression.
Parameters:
Name | Type | Description |
---|---|---|
body |
Object | a valid regular expression unit. |
quantifiers |
String | the quantifiers applied on this term. |
- Source:
Methods
-
(protected, static) charClasses(list, positive, warnings)
-
Quote the terms so they can be put into character classes (square brackets).
Parameters:
Name Type Description list
Array the input term(s) to convert.
positive
Boolean treat for positive or negative character classes.
warnings
Array [output] a collection array to keep errors / warnings while converting character classes.
- Source:
Returns:
a single character sequence that can fit into character classes.
-
(protected, static) isUnitTerm(expression) → {Boolean}
-
Test if the given expression is a unit term.
Parameters:
Name Type Description expression
String the expression string to test.
- Source:
Returns:
true is the given expression is a unit term.
- Type
- Boolean
-
(protected, static) quote(value) → {String}
-
Quote regular expression characters.
Takes string and puts a backslash in front of every character that is part of the regular expression syntax.
Parameters:
Name Type Description value
String the string to quote.
- Source:
Returns:
the quoted string.
- Type
- String
-
(protected, static) sanitize(body, quantifiers) → {Term}
-
Sanitation function for adding anything safely to the expression.
Parameters:
Name Type Description body
Object the expression object to sanitize.
quantifiers
String the quantifiers applied on this term.
- Source:
Returns:
a new Term object with contents sanitized.
- Type
- Term
-
(protected, static) wrap(body) → {String}
-
Wrap the given expression if it is not a unit term.
Parameters:
Name Type Description body
String the expression string to test.
- Source:
Returns:
a unit expression that is properly protected.
- Type
- String
-
(protected) _generate(context, termRequiresWrap) → {String}
-
important: _generate and _generateBody should never modify the term object.
implementation notes:
termRequiresWrap tells fragile term(s) in sub-expression that if protection is required. There are 2 situations: 0.no: If there is only one term, then the terms need not protection at all. 1.maybe: If the sub-expression is composed with more then one term, and the terms will be evaluated in order, i.e., will be concatenated directly, then the terms need not protection, unless it is the "either" expression.
[in traditional chinese]
termRequiresWrap 是要通知元素是否需要使用 group 來保護內容。
有兩種狀況:
0.no: 元素沒有兄弟元素(僅有一個子元素),則元素本身不需要特別保護。 1.maybe: 有兄弟元素,且兄弟元素之間將直接接合(concatenated), 元素應視需要(目前只有 either 運算式有此需要)自我保護。
Parameters:
Name Type Description context
Object the context object of the regexGen generator.
termRequiresWrap
Number should the term requires wrap. See possible values descripted above.
- Source:
Returns:
the generated regular expression string literal.
- Type
- String
-
(protected) _generateBody(context, bodyRequiresWrap) → {String}
-
Parameters:
Name Type Description context
Object the context object of the regexGen generator.
bodyRequiresWrap
Number should the body of term requires wrap. See possible values descripted in #_generate.
- Source:
Returns:
the generated regular expression string literal (body part).
- Type
- String
-
(protected) _init(body, quantifiers)
-
Initialize the term object, setup default values.
Parameters:
Name Type Description body
String the expression string.
quantifiers
String the quantifiers applied on this term.
- Source:
-
(protected) _warn(msg, values)
-
Parameters:
Name Type Description msg
String -
values
Object -
- Source:
-
any()
-
Matches the expression generated by the preceding sub-generator 0 or more times. Equivalent to
/(.*)/
and/.{0,}/
.- Source:
-
contains(…terms)
-
Parameters:
Name Type Attributes Description terms
Object <repeatable>
-
- Source:
-
followedBy(…terms)
-
Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead. (x(?=y))
Parameters:
Name Type Attributes Description terms
Object <repeatable>
-
- Source:
-
greedy()
-
Makes a quantifier greedy. Note that quantifier are greedy by default.
- Source:
Example
anyChar().any().greedy() // ==> /.\u002A/ anyChar().many().greedy() // ==> /.+/ anyChar().maybe().greedy() // ==> /.?/
-
lazy()
-
Makes a quantifier lazy.
- Source:
Example
anyChar().any().lazy() // ==> /.*?/ anyChar().many().lazy() // ==> /.+?/ anyChar().maybe().lazy() // ==> /.??/ anyChar().multiple(5,9).lazy() // ==> /.{5,9}?/
-
many()
-
occurs one or more times (x+)
- Source:
-
maybe()
-
occurs zero or one times (x?)
- Source:
-
multiple()
-
occurs at least min times and (optional) at most max times (?||+|{min,}|{min,max}) occurs at least min times and (optional) at most max times (?||+|{min,}|{min,max})
- Source:
-
notContains(…terms)
-
Parameters:
Name Type Attributes Description terms
Object <repeatable>
-
- Source:
-
notFollowedBy(…terms)
-
Matches 'x' only if 'x' is not followed by 'y'. This is called a negated lookahead. (x(?!y))
Parameters:
Name Type Attributes Description terms
Object <repeatable>
-
- Source:
-
regex(value)
-
Use the given regex, i.e., trust me, just put the value as is.
Parameters:
Name Type Description value
RegExp | String - Source:
Example
regex( /\w\d/ ) // ==> /\w\d/ regex( "\\w\\d" ) // ==> /\w\d/
-
reluctant()
-
This is an alias of
lazy()
.- Source:
-
repeat()
-
occurs at least once or exactly specified times (+|{n})
- Source: