REGEX to split comma delimited string capturing empty strings for all blank sections

Viewed 203

I found some javascript regex that captures all comma delimited fields even if they are empty:

(("[^"]*")|[^,]+)|(?=,(,|$))

However, it doesn't capture first field if its empty. Test it here: https://regex101.com/r/h7vb4U/1

Anyone know how I can make it capture the first empty field?

1 Answers

You can use

"([^"]*)"|([^,]+)|(?<=,|^)(?=,|$)

See the regex demo.

Details

  • "([^"]*)" - a substring in between double quotation marks that cannot have other double quotation marks
  • ([^,]+) - 1+ non-comma chars
  • (?<=,|^)(?=,|$) - a location between a comma or start of string and a comma or end of string.
Related