Javascript regex for a comma seperated sequence of numbers

Yui

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. ;-)

Jaromanda X

you were so close

/^(\d+,){1,5}\d*$/
 ^ ^         ^ ^
 1 2         3 4
  1. the beginning of line needs to be outside the parenthesis
  2. the d should be \d
  3. the d should be \d
  4. ? = 0 or 1, * = 0 or more (if the last number can only be a single digit, then use ? like you did

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex for word space comma seperated numbers

From Dev

Using Regex in C# to check for optional comma seperated repeating sequence

From Dev

RegEx for comma seperated list

From Dev

Making a regex expression for adding single quotes in numbers comma seperated

From Dev

Removing value from a comma seperated list with Regex

From Dev

Regex to allow a comma seperated list of codes

From Dev

Regex expression - one or multiple strings seperated by comma

From Dev

How to print the numbers separately (comma-seperated) using Python?

From Dev

regex for numbers comma and space

From Java

Regex with comma delimited string seperated by pipe sign in .NET

From Dev

RegEx - Extract words that contains a substring, from a comma seperated string

From Dev

Regex got Splitting comma seperated String ignoring commas in double quotes

From Dev

RegEx - Extract words that contains a substring, from a comma seperated string

From Dev

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

From Dev

How to split a comma seperated string and insert into html using javascript

From Dev

How to split a comma seperated string and insert into html using javascript

From Dev

Check if entries are seperated by comma

From Dev

Edit comma seperated fields

From Dev

Implementing regex in comma separated numbers

From Dev

Find repeated numbers sequence with regex

From Dev

How to use GROUP_CONCAT in mySQL when one of the fields contain comma seperated numbers?

From Dev

How to capture a variably-long, comma-seperated list of strings via Regex?

From Dev

Turn comma seperated string into an array

From Dev

store value in a variable seperated by comma (,)

From Dev

PySpark Aggregation on Comma Seperated Column

From Dev

How can I check if a string contains only numbers, comma and periods with regex in javascript?

From Dev

Regex for extracting comma delimited numbers in brackets

From Dev

Regex for matching specific numbers separated by comma

From Dev

Regex for extracting comma delimited numbers in brackets

Related Related

  1. 1

    Regex for word space comma seperated numbers

  2. 2

    Using Regex in C# to check for optional comma seperated repeating sequence

  3. 3

    RegEx for comma seperated list

  4. 4

    Making a regex expression for adding single quotes in numbers comma seperated

  5. 5

    Removing value from a comma seperated list with Regex

  6. 6

    Regex to allow a comma seperated list of codes

  7. 7

    Regex expression - one or multiple strings seperated by comma

  8. 8

    How to print the numbers separately (comma-seperated) using Python?

  9. 9

    regex for numbers comma and space

  10. 10

    Regex with comma delimited string seperated by pipe sign in .NET

  11. 11

    RegEx - Extract words that contains a substring, from a comma seperated string

  12. 12

    Regex got Splitting comma seperated String ignoring commas in double quotes

  13. 13

    RegEx - Extract words that contains a substring, from a comma seperated string

  14. 14

    JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

  15. 15

    How to split a comma seperated string and insert into html using javascript

  16. 16

    How to split a comma seperated string and insert into html using javascript

  17. 17

    Check if entries are seperated by comma

  18. 18

    Edit comma seperated fields

  19. 19

    Implementing regex in comma separated numbers

  20. 20

    Find repeated numbers sequence with regex

  21. 21

    How to use GROUP_CONCAT in mySQL when one of the fields contain comma seperated numbers?

  22. 22

    How to capture a variably-long, comma-seperated list of strings via Regex?

  23. 23

    Turn comma seperated string into an array

  24. 24

    store value in a variable seperated by comma (,)

  25. 25

    PySpark Aggregation on Comma Seperated Column

  26. 26

    How can I check if a string contains only numbers, comma and periods with regex in javascript?

  27. 27

    Regex for extracting comma delimited numbers in brackets

  28. 28

    Regex for matching specific numbers separated by comma

  29. 29

    Regex for extracting comma delimited numbers in brackets

HotTag

Archive