JSVerbalExpressions
JSVerbalExpressions copied to clipboard
SyntaxError: unterminated parenthetical
From using:
VerEx().startOfLine().then("aaa").or("bbb").endOfLine()
I'd expect it to compile to something like:
/^(aaa|bbb)$/
...but since endOfLine
discards existing suffixes (where there is a capture group close parenthesis from .or
), it leaves the capture group open, resulting in:
/^(aaa|bbb$/
The same error here: SyntaxError: Invalid regular expression: /^(?:(?:aaa))|(?:(?:bbb)$/: Unterminated group
The .or()
function generates the capture group's open parenthesis in _prefixes: '^(?:'
, the corresponding capture group's close parenthesis in source: _source: '(?:aaa))' before the .or()
function, but the capture group's close parenthesis of the .or
function is generated by the suffixes variable => _suffixes: ')$'.
So then, the problem is when we use endOfLine
, it generates an empty prefixes _prefixes: ''
and the suffixes _suffixes: '$'
which deletes the previous suffixes which includes the capture group's close parenthesis generated by the .or
function.
My solution for now was the following below:
In the endOfLine
function I've added the addition assignment operator +=
to the _suffixes
variable instead of the assignment operator =
so that we add instead of rewrite the _suffixes variable. example:
Before adding addition assignment operator to the _suffixes variable:
{
key: "endOfLine",
value: function endOfLine() {
var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
this._suffixes = enable ? '$' : '';
return this.add();
}
After adding addition assignment operator to the _suffixes variable:
{
key: "endOfLine",
value: function endOfLine() {
var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
this._suffixes += enable ? '$' : '';
return this.add();
}
Also you can test it out with this examples to see the three different outputs:
const VerEx = require('../dist/verbalexpressions.js');
// or and endOfLine
const expr1 = VerEx().startOfLine().then("aaa").or("bbb").endOfLine()
console.log(expr1);
// endOfLine
const expr2 = VerEx().find('aaa').endOfLine();
console.log(expr2);
// or
const expr3 = VerEx().find('aaa').or('bbb');
console.log(expr3);
I hope this can help.