0

I found a lot of questions about simple splits, but my file is similar to a csv file and some strings are between quotes and may contain commas.

Sample:

myKey,this is a string,"This is yet, another, string"

Desired output:

["myKey", "this is a string", "This is yet, another, string"]

I found a similar question but I was unable to adapt it properly:

str.split(/([^,"]+|[^,"]*)+/g)[0]

It gives me the first character of the string instead. I also tried adding ?: after the first parenthesis like I saw, but it didn't seem to matter.

HypeWolf
  • 750
  • 12
  • 29
  • 4
    I wouldn't reinvent this, just use e.g. https://www.papaparse.com/ – jonrsharpe Jan 18 '20 at 19:25
  • 1
    What if the quoted sentence has quotes in it? – trincot Jan 18 '20 at 19:37
  • 2
    Does this answer your question? [Split a string by commas but ignore commas within double-quotes using Javascript](https://stackoverflow.com/questions/11456850/split-a-string-by-commas-but-ignore-commas-within-double-quotes-using-javascript) – user2222 Jan 18 '20 at 20:07
  • Is it similar to a CSV file, or is it literally a CSV file? – Ry- Jan 18 '20 at 20:17

2 Answers2

1

If there are no special cases with escaped quotes within quotes, and the input is valid, then you could use this:

var s = 'myKey,this is a string,"This is yet, another, string"';
var result = Array.from(s.matchAll(/[^",]+|"([^"]*)"/g), ([a,b]) => b || a);
console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
1
'myKey,this is a string,"This is yet, another, string"'
    .split(/,(?=(?:[^"]*"[^"]*")*$)/g);
// ["myKey", "this is a string", "\"This is yet, another, string\""]

This works by making sure there are zero or more pairs of quote chars ahead of the comma until the end of the string. If there are an odd number of quotes ahead the comma it will not be matched since it should be within a quote provided all quotes are closed in the string.

Thomas Brierley
  • 1,187
  • 1
  • 7
  • 8