I need a regex in javascript for a pattern. This pattern looks like followed:
(any number followed by a comma) --> 1..5 times followed optionally by any number
some valid examples:
1002
1001,1002,1003,21313,3333
912393,4951131313,92812,3131,43434,13123
some invalid examples:
a
some_string1, ,somestring2,32232
1,2,3,4,5,6,7
100,200,300,
As you can see by the given examples there is the following rule:
number->comma->number => valid
number => valid
number->comma => invalid
anything else than a number => invalid
more than six numbers => invalid
I already tried to find a solution by myself but without any luck.
I created this here:
/(^d+,){1,5}d?$/
But unfortunately it does not work. By the way: I'm not really into regular expression, I'm gonna try to practice it in the future. ;-)
you were so close
/^(\d+,){1,5}\d*$/
^ ^ ^ ^
1 2 3 4
the above matches one wrong pattern, 1,2,3,4,5,
would match!!
/^(\d+,|){1,4}\d+(,\d+|)$/
that's better (it works 100%, but probably not the optimal solution)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments