Javascript: Regex for Parsing Google Sheets Data Containing Commas

I have a JS script that pulls data from a public Google Sheets feed which returns data in a sort of JSON-CSV format I need to parse.

Rows are comma-separated but commas inside each item are not escaped, e.g:

“a: Feb 21, 10:11, b: some content, c: more, d: even more”

Desired outcome:

{
  "a": "Feb 21, 10:11",
  "b": "some content",
  "c": "more",
  "d": "even more"
 }

Split attempt

data.split(‘,’) returns:

{
  "a: Feb 21,
  "10:11",
  "b some content",
  "c: more",
  "d: even more"
 }

Regex attempt

The following regex is the closest I could get but it still wraps the comma but I need it to wrap the date to the beginning of the next occurrence of a property: (?[^,]+): (?[^,]+)

Attempt imageRegex101

Answer

Here is a way to go:

(?<tag>w+): (?<value>.+?)(?:,(?= w+:)|$)

Demo & explanation

Leave a Reply

Your email address will not be published. Required fields are marked *