OR
a fraction with a slash must be compulsorily 3 characters with slash at the 2nd character and the numerator[1-8] always smaller than the denominator[2-9] so minimum possible fraction being 1/2. so maximum possible fraction being 8/9. valid fractions include : 2/3, 5/8, 4/9 etc.
invalid fractions which cannot be included are 3/400 8/5 6/3 0/9
What would be the regex?
const req = /someregex/g;
also how does req.test(string) work?
There is no short regular expression for that, since there is no way to say that the numerator should be smaller than the denominator. Luckily you only want to accept numerators and denominators with just one digit, so you can list allowed denominators for each numerator: there are just 8 possibilities for the numerator.
So combined with the integer between 1 and 99, you get this:
^([1-9]\d?|1\/[2-9]|2\/[3-9]|3\/[4-9]|4\/[5-9]|5\/[6-9]|6\/[7-9]|7\/[89]|8\/9)$
The ^
and $
anchors make sure the entire string should match -- so no characters are allowed around a potential match.
Although this may work for your case, in general I would say regular expressions are not the right tool for this. Consider coding your validation in JavaScript.