Wednesday 6 January 2021

How to write a range syntax in Peg JS

I was trying to write syntax to validate the numbers between 1 and 128 but couldn't accomplish the task in peg.js although it worked in the regular expression compatible with the Javascript.

Regular Expression: ^(12[0-8]|1[01][0-9]|[1-9]?[1-9])$

But couldn't replicate the same expression in the peg.js.

numberRange = l: (rangeRegex) m:(integer*)  {
if(m.length !== 0){
l = l + m.join("");
}
return l;
}

rangeRegex = ^(12[0-8]|1[01][0-9]|[1-9]?[1-9])$

I tried to use the same regular expression in the Peg.js as well which throws the Expected "!", "$", "&", "(", ".", character class, comment, end of the line, identifier, literal, or whitespace but "^" found.



from How to write a range syntax in Peg JS

No comments:

Post a Comment