Ashlin Ashlin - 2 months ago 6
Javascript Question

Regex: Comma separated values

I have been trying to make a regex that will: match only if all condition are met, will not match 1 to 10 or something like it and will ignore commas.

I have made

(?=.*1)(?=.*5)
which almost works and will match to
1,5
as it is meant to but will also match to
10,5
,
10,50
and
1,50
. I can not work out how to stop this. So my question is how do I get the regex to know the difference between numerals and a single digit.

Answer

Make it match word boundaries around the number, using \b.

(?=.*\b1\b)(?=.*\b5\b)