Reputation: 633
I want to get range values using RegEx on Javascript so that when I use .match
it would return the values. I've use -
or ~
as range operator.
My current regex is /(\-?\d+)(?:\-|\~)(\-?\d+)/
but this will not return the number with decimal value.
Successful get:
"5-10"
should return ["5-10", "5", "10"]
"-5-10"
should return ["-5-10", "-5", "10"]
"-5--10"
should return ["-5-10", "-5", "-10"]
Failed to get:
"5.1-10"
should return ["5.1-10", "5.1", "10"]
"-5.1-10"
should return ["-5.1-10", "-5.1", "10"]
"-5.1--10"
should return ["-5.1-10", "-5.1", "-10"]
"-5.1--10.2"
should return ["-5.1-10", "-5.1", "-10.2"]
Upvotes: 1
Views: 583
Reputation: 1431
/^(-?\d+\.?\d*?)[-~](-?\d+\.?\d*?)$/
You need to capture the first digit which may have negative sign -
and decimal with digit .digit
. Then separate by either hyphen -
or tilde ~
, finally capture the second digit with the same manner as the first one.
Upvotes: 1