RegexWars
Tier 4 · Enchanterextract

Split The CSV Row

Extract the fields of a CSV row, where a quoted field may contain commas. Quoted fields keep their quotes.

Splitting on commas is the bug everyone ships. Extract the fields instead: either a quoted run, or a run of anything that is not a comma. The order of your alternatives decides whether this works. Two things this deliberately does not handle, because matchAll cannot: an empty field between two commas has nothing to match, so it vanishes rather than coming back as "", and a doubled quote inside a quoted field ends the field early. Real CSV parsers are state machines for exactly these two reasons — which is the honest lesson about where regex stops.

Your pattern1 chars · par 14
//g
Flags
Start typing — tests run as you go.0 of 5 visible tests passing
Test cases
  • one,two,three→ ["one", "two", "three"]
  • a,"b,c",d→ ["a", "\"b,c\"", "d"]
  • "hello, world",42→ ["\"hello, world\"", "42"]
  • solo→ ["solo"]
  • a,not quoted,b→ ["a", "not quoted", "b"]

    an unquoted field can contain spaces

+ 4 hidden tests, checked when you submit. They are what stops a pattern that only fits the examples above.