1

I have a single string, which is itself a comma delimited list of quoted strings - which can have commas within them.

Example:

var str = '"long one","short","oh, look","wow.", ""';

I need to split this into an array:

['long one', 'short', 'oh, look', 'wow.', '']

// will take this if it is easier
['"long one"', '"short"', '"oh, look"', '"wow."', '""']

I tried splitting by , but it throws off entries with a comma in them. I also tried splitting by \",, but it creates lob-sided string:

['"long one', '"short', '"oh, look', '"wow.', '"']

I tried splitting by \",\" but causes the same issue but just for the first and last entry:

['"long one', 'short', 'oh, look', 'wow.', '"']

I also tried the regex expression found in this answer, but it adds an empty entry at the start and end of the array:

['', '"long one"', '"short"', '"oh, look"', '"wow."', '""', '']

Any suggestions?

Thanks.

Community
  • 1
  • 1
Mac
  • 1,143
  • 6
  • 21
  • 45
  • 1
    Why not use the regex you found then remove the first and last elements? – Mike Cluck Aug 29 '16 at 15:16
  • 4
    Match [`"(.*?)"`](https://regex101.com/r/hP4kG9/1) – Tushar Aug 29 '16 at 15:17
  • Do these come from a csv file? – Tamas Hegedus Aug 29 '16 at 15:19
  • yes, they do come from a CSV file. @MikeC, you're right, I could do that. I suppose I just didn't want to risk lobbing of some data - on the off chance the extra entries were not attached – Mac Aug 29 '16 at 15:40
  • 1
    Since quotes don't appear in quoted strings, and your data is delimited by quotes, it's really as simple as finding all `"([^"]*)"`. This is the simplest solution. If you later add caveats to this simple description, a fuller regex is necessary. –  Aug 29 '16 at 15:50

3 Answers3

7

You could treat it as part of a JSON string and append the necessary parts for parsing as array.

var string ='"long one","short","oh, look","wow.", ""',
    array = JSON.parse('[' + string + ']');
console.log(array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 2
    With the caveat that any special characters are in there the same as they are in JSON - tabs, line breaks, etc. For example, if double-quotes are escaped as double-double quotes instead of `\"`, then this won't work. – Joe Enos Aug 29 '16 at 15:20
  • right, that would be a problem, but it does not look like this in the question. – Nina Scholz Aug 29 '16 at 15:23
  • Thanks for the heads up @Joe. I think this solution works just fine for my case though. – Mac Aug 29 '16 at 15:44
  • Yep, it's a very good solution. Just always good to keep the edge cases in mind. – Joe Enos Aug 29 '16 at 15:45
0

You can .split() comma , characters if comma is followed by double-quote " character using RegExp /,(?=")/

str.split(/,(?=")/)
guest271314
  • 1
  • 15
  • 104
  • 177
0

var s = '"long one","short","oh, look","wow.", ""';
var answer = s.split(/("*,.?")/gi).filter(function (a) {
  return a.replace(/"/g, '').length > 2
}).map(function (a) {
  return a.replace(/"/g, '')
});
console.log(s);
console.log(answer);
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28