You must have two (or more) separate expressions (using the |
operator) in order to do that.
So it would be something like this:
/\[\s*("[^"]*"|[0-9]+)(\s*,\s*("[^"]*"|[0-9]+))*\s*\]/
(You may also want to use ^
at the start and $
at the end to make sure nothing else appears before/after the array: /^...snip...$/
to match the string from start to finish.)
If you need floating point numbers with exponents, add a period and the 'e' character: [0-9.eE]+
(which is why I did not use \d+
because only digits are allowed in that case.) To make sure a number is valid, it's much more complicated, obviously (sign, exponent with/without sign, digits only before or after the decimal point...)
You could also support single quoted strings. That too is a separate expression: '[^']*'
.
You may want to allow spaces before and after the brackets too (start: /^\s*\[...
and end: ...\]\s*$/
).
Finally, if you want to really support JavaScript strings you would need to add support for the backslash. Something like this: ("([^"]|\\.)*")
.
Note
Your .+
expression would match "
and ,
too and without the ^
and $
an array as follow matches your expression just fine:
This Array ["test", 123, true, "this"] Here