rathindu_w
rathindu_w

Reputation: 881

Javascript Regex doesn't match the recurring pattern

Input:"[3, 4]", "[1, 2, 7, 7]"
Input:"[13, 4]", "[1, 2, 3, 6, 14]"
Input: "[5, 5]", "[1, 2, 3]"

\"\s*\[[0-9]\s*\,\s*[0-9]\]\"\s*\,\s*\"\[[0-9]\s*\,

This was what I tried to validate the above inputs. with what I tried I couldn't get the last part of the string validated. the second array of data can be any number of inputs. The above regex applies until the first comma of the second array. couldn't now write a general expression for any number of inputs after that.

Upvotes: 0

Views: 45

Answers (1)

user13843220
user13843220

Reputation:

If I understand correctly

^\s*"\s*\[\s*[0-9]+\s*(?:\,\s*[0-9]+\s*)*\]\s*"(?:\s*,\s*"\s*\[\s*[0-9]+\s*(?:\,\s*[0-9]+\s*)*\]\s*")*\s*$

https://regex101.com/r/PpZy8I/1

 ^                  # Begin of string     
 \s*                # Leading wsp
 " \s*              # Quote start of array
 \[                 # Array opening
 \s* [0-9]+ \s* 
 (?:                # Optional nesting elements comma plus digits
    \, \s* 
    [0-9]+ \s* 
 )*
 \]                 # Array close
 \s* 
 "                  # Quote end of array    
 
 (?:                # Optional many more arrays
    \s* , \s* 
    " \s* 
    \[ 
    \s* [0-9]+ \s* 
    (?:
       \, \s* 
       [0-9]+ \s* 
    )*
    \] 
    \s* 
    "     
 )*
 \s*                # Trailing wsp
 $                  # End of string

Upvotes: 1

Related Questions