I'm looking for [a, b, c, "d, e, f", g, h]
to turn into an array of 6 elements: a, b, c, "d,e,f", g, h. I'm a bit of a noob with RegEx so any help is great. I'm trying to do this through Javascript. This is what I have so far:
str = str.split(/,+|"[^"]+"/g);
But right now it's splitting out everything that's in the double-quotes, which is incorrect. Thanks for any help.
Edit: Okay sorry I worded this question really poorly. I'm being given a string not an array.
var str = 'a, b, c, "d, e, f", g, h';
And I want to turn that into an array using something like the "split" function.
I know it's a bit long, but here's my take:
As a side note, it will work both with, and without the braces in the start and end.
Here's what I would do.
This works well for me. (I used semicolons so the alert message would show the difference between commas added when turning the array into a string and the actual captured values.)
Something like a stack should do the trick. Here I vaguely use marker boolean as stack (just getting my purpose served with it).
Here is a JavaScript function to do it:
Assuming your string really looks like
'[a, b, c, "d, e, f", g, h]'
, I believe this would be 'an acceptable use case foreval()
:Edit: As Rocket pointed out,
strict
mode removeseval
's ability to inject variables into the local scope, meaning you'd want to do this: