Reputation: 1585
In javascript I need to parse a user input. The input is of the format: number - number
. This can be repeated and separated by commas.
The following are some examples:
1-10
4-10,13-17
6-10,3-8,4-12
Here is the regex I wrote for this
(\d+[-]\d+[,]?)
However, there are 2 constraints.
Is there a way I can enforce these constraints with regex?
Upvotes: 0
Views: 178
Reputation: 4854
While you can certainly match the format with regexes, you can't do the kind of verification you want with them. What I would recommend is something like this (in JS):
function verifyList(list) {
var matches = list.match(/\d+-\d+/g);
for (match in matches) {
var numbers = match.match(/(\d+)-(\d+)/);
if (numbers[1] >= numbers[2]) return false;
}
return true;
}
Upvotes: 2