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.
Here's a non-regex one that assumes doublequotes will come in pairs:
jsfiddle setting image code output image
The code works if your input string in the format of stringTocompare. Run the code on https://jsfiddle.net/ to see output for fiddlejs setting. Please refer to the screenshot. You can either use split function for the same for the code below it and tweak the code according to you need. Remove the bold or word with in ** from the code if you dont want to have comma after split attach=attach**+","**+actualString[t+1].
regex:
/,(?=(?:(?:[^"]*"){2})*[^"]*$)/
Reference following link for an explanation: regexr.com/44u6o
I've had similar issues with this, and I've found no good .net solution so went DIY. NOTE: This was also used to reply to
Splitting comma separated string, ignore commas in quotes, but allow strings with one double quotation
but seems more applicable here (but useful over there)
In my application I'm parsing a csv so my split credential is ",". this method I suppose only works for where you have a single char split argument.
So, I've written a function that ignores commas within double quotes. it does it by converting the input string into a character array and parsing char by char
This function, to my application taste also ignores ("") in any input as these are unneeded and present in my input.