Home > OS >  Regex for a single range or comma separated string of numbers
Regex for a single range or comma separated string of numbers

Time:08-05

I have a text field that I want to only accept a single range of numbers (eg. "1000-3000") OR a comma-separated list of numbers (eg. "1003", "1004", "2000") but not both. Some examples of expected results:

1000 (valid)
1000, 2000 (valid)
1000 ,2000 (valid)
1000, 2000, (valid)
,1000, 2000 (invalid)
1000,1000 (valid)
1000-2000, 3000-3500 (invalid).
1000-2000, 3000 (invalid).
1000 - 2000 (valid)
1000-2000 (valid)
,1000 (invalid)

This is what I have which doesn't work fully:

let input = "1000, 5000-6000"
input.match(/^(([0-9\s](,)?(-)?)*) $/); // returns a match

input = "1000 - 3 000"  
input.match (/^(([0-9\s](,)?(-)?)*) $/)// returns a match (space in second num)

let input = "5000-"
input.match(/^(([0-9\s](,)?(-)?)*) $/); // returns a match

My version might fail in other ways I haven't found yet, but those 3 examples of matches shouldn't work. I'm using javascript/react.

CodePudding user response:

You may use the following regex pattern, which matches a number either followed by a hyphen and another number (single range), or by comma and and another number, any number of times (CSV list).

^\d (?:(?:\s*-\s*\d )?|(?:\s*,\s*\d )*\s*,?)$

Sample script:

var inputs = ["1000", "1000, 2000", "1000 ,2000", "1000, 2000,", "1000,1000", "1000 - 2000", "1000-2000", ",1000, 2000", "1000-2000, 3000-3500", "1000-2000, 3000", ",1000"];
inputs.forEach(x => console.log(x   " => "   /^\d (?:(?:\s*-\s*\d )?|(?:\s*,\s*\d )*\s*,?)$/.test(x)));

  • Related