Jeff Robert Dagala
Jeff Robert Dagala

Reputation: 633

How to get range value from string using RegEx on Javascript

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:

Failed to get:

Upvotes: 1

Views: 583

Answers (1)

ACD
ACD

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

Related Questions