Reputation: 738
I have quite small but very annoying problem with regex. I would like to find regex for comma separated list which contains nine digits phone number for example :
Pass : 123456789,123456789
Not Pass : 123456789,123456789,
So far, I have something like this :/^\d{9}+(,\d{9}\+)\*$/
Of course it works for example in this tool http://regex.larsolavtorvik.com, but in javascript it does not work and I get this I suppose well known error (for Javascript people) :
Invalid regular expression: /^\d{9}+(,\d{9}\+)\*$/: Nothing to repeat
So, I added backslash and it looks like this one : /^\d{9}\+(,\d{9}\+)\*$/
. Of course this one also does not work.
Upvotes: 2
Views: 1482
Reputation: 32807
You are escaping *
,+
with \
.That is the problem..
*
means match the preceding char 0 to many times
+
means match the preceding char 1 to many times
{9}
means match the preceding char 9 times..so there is no need of using +
after it
The regex should be
/^\d{9}(,\d{9})*$/
Upvotes: 3