Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex compare two numbers

can i somehow compare two numbers in regex? i want regex that is correct for 10-12, but incorrect for 12-10. I mean that 10 must be smaller than 12. I want to do it in Javascript.


2 Answers

If the input is always of the form X-Y, then why not use the split() function with '-' as the delimiter and then compare the two parts with >

You can't compare numerical values using RegExps.

like image 99
Adam Pope Avatar answered Mar 10 '26 14:03

Adam Pope


I wouldn't use regex for this. I'd split the string on the operator, then compare the two resulting numbers based on what operator I found (I'm assuming 10+12 and 12+10 would both be legal).

like image 22
Bill the Lizard Avatar answered Mar 10 '26 14:03

Bill the Lizard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!