Reputation: 1995
I am using angularjs and curently have the following REGEX
ng-pattern = "/^[0-9,\b\s]+$/"
This regex works to a degree although I am unable to find how to alter it to detect white space between numbers, but at the same time allow white space between commas.
ie
123 // is ok
123, 345, 5453 // is ok
123 345, 5453 // is not ok
123, 345, 5453, // is not ok
,123, 345, 5453 // is not ok
Any ideas?
Upvotes: 1
Views: 894
Reputation: 785128
You can use this regex:
^(?:\d+(?:, *|))*\d+$
Upvotes: 1
Reputation: 74257
This regular expression
^\d{1,3}(,\s*\d\d\d)*$
Matches
Which is to say...any number written in the conventional group-delimited form using commas as the group delimiter and allowing trailing whitespace following the commas:
1
12
123
1,234
12, 123,456
All match, but things like
123456
123,4
1 234
If you wanted to make the group separators optional, it's an easy modification:
^\d{1,3}((,\s*)?\d\d\d)*$
This will also match numbers like
12345
12, 3456
Etc.
Upvotes: 1
Reputation: 30995
You can use this regex:
^(?:\d+,\s*)+\d+$
http://regex101.com/r/xE2sQ3/1
123, 345, 5453 // OK
123 345, 5453
123, 345, 5453 // OK
123, 345 5453,
Upvotes: 1