0

I have written a small regex for javascript. It should only accept numbers separated by commas.

Valid examples are:

1 single value allowed
1,278,3780,50
1,56,90, (trailing comma allowed)

Invalid examples are:

1,45 67

1, gj, + (any special character and characters)

The regex is: /^[\d|\,]+/g

However, it also accepts | (pipe character).

Like: 1|46|6778|567

What am I doing wrong? What did I miss? Please follow this link to my regex

Shekhar Joshi
  • 958
  • 1
  • 13
  • 26
  • possible duplicate of [Regex for Comma Separated Number](http://stackoverflow.com/questions/1359147/regex-for-comma-separated-number) – Anonymous Jun 03 '15 at 13:04

4 Answers4

4

You don't need pipe (|) and escaping characters within character class. Also as a proper way you can use following regex:

 /^(?:\d+\,)+\d+$/g

Regular expression visualization

Debuggex Demo

As i missed your edit if the trailing comma is a valid case you can simply use following regex :

^(\d+,?)+$
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • What about last comma – Tushar Jun 03 '15 at 12:59
  • your regex also allows this input 57,,,,,,578678,678,67,678,678,,,,,6,78,6 My requirement involves only one comma per separation – Shekhar Joshi Jun 03 '15 at 13:00
  • I ended up writing this regex `^(?:\d+\,)*\d+\,{0,1}$` I still don't know what `?:` this is...I know its called a capturing group but what it does and when to use it know idea! – Shekhar Joshi Jun 03 '15 at 13:33
  • Go with @Andie2302's answer (http://stackoverflow.com/a/30621011/4044587). This does not match your requirement to match a single value and should not be marked as the accepted answer. – md4 Jun 03 '15 at 13:36
  • @ShekharJoshi Its none capture grouping and makes you regex engine doesn't return the matched pattern by that group – Mazdak Jun 03 '15 at 13:38
  • @md4 That answer is incorrect because it match comma in end of string!!! and about my answer yes it doesn't match single number but OP wants numbers separated with comma!!!! – Mazdak Jun 03 '15 at 13:43
  • The OP says that a trailing comma is a *valid* example. Also, go easy on the exclamation marks. It looks like you're yelling. – md4 Jun 03 '15 at 13:45
  • @kasramvd I know its an old question. But for learning, Can you explain what you meant by the first sentence in your answer? – KiraAG Oct 21 '19 at 06:57
  • 1
    @KiraAG Character class is for matching a combination of characters which makes use of logical or obsolete. Plus there's no need to escape regex characters too like `[\*]` is the same as `[*]`. The character class escape those characters automatically. The only exception is close bracket which should be escaped to not be mistaken with the character class's closing bracket. – Mazdak Oct 21 '19 at 09:55
2

Try this -

^\d+\,(?:\d+\,?)+$

Demo

EDIT:
With changed requirements -

^\d+(?:,\d+)*,?$

Demo here

Kamehameha
  • 5,423
  • 1
  • 23
  • 28
  • This fails to match single values (with or without trailing commas). – md4 Jun 03 '15 at 13:34
  • @md4 Yep, The question was edited after I wrote the answer. `Single value not allowed` was added after I answered. Will add that for completeness. Thanks :) – Kamehameha Jun 03 '15 at 17:22
  • I realised that after I said the same thing to @Kasra. I really do need to pay attention to question edits in future. – md4 Jun 04 '15 at 00:40
1

To match a number separated by comma:

(\d+,?)+
Andie2302
  • 4,825
  • 4
  • 24
  • 43
  • Upvoted for the sheer simplicity. For completeness sake, it should be anchored to avoid matching invalid strings that merely contain the delimited number sequence. – md4 Jun 03 '15 at 13:31
  • @Kasra, as stated above, that is a *valid* example as per the OP's requirements. – md4 Jun 03 '15 at 13:47
1

The correct regex is as follows:

^\d+(?:,\d+)*,?$

This will match the cases specified:

  • A single number (1)
  • A series of numbers delimited by commas (1,2,3)
  • An optional trailing comma (1,, 1,2,3,4,)
md4
  • 1,609
  • 11
  • 13