Stack Overflow Asked by Hemraj Rijal on March 3, 2021
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.
We couldn't match the regex directly in peg.js however, we are allowed to use the regex inside the predicate of peg.js to validate the number. And we do have the privilege to throw the custom error if the number does not match the regex provided.
numberRange = l: (rangeRegex* ) {
if (l.length > 0) {
l = l.join("");
}
if (l.match('^(12[0-8]|1[01][0-9]|[1-9][0-9]?)$')) {
return l;
} else {
error("The number must be in a range of 1 to 128");
}
}
rangeRegex =[0-9]
Following code helped me to validate the number within the range of 1 to 128. I hope this helps others as well.
Correct answer by Hemraj Rijal on March 3, 2021
Here’s another solution without regex:
numberRange = "1" [0-2] [0-8] / [1-9] [0-9] / [1-9]
Answered by Josh Voigts on March 3, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP