Community JavaScript Snippet

CSV With Quoted Commas: The 30-Line Parser

split(',') gets you fired on the first row that contains a comma inside quotes. Here is a state-machine CSV parser in 30 lines that handles quoted commas, escaped quotes, and CRLF endings.

CSV With Quoted Commas: The 30-Line Parser

split(',') gets you fired on the first row that contains a comma inside quotes. Here is a state-machine CSV parser in 30 lines that handles quoted commas, escaped quotes, and CRLF endings.

JavaScript
Frontend
3 snippets
serialization
code-template
input-output
gracechoi

By @gracechoi

December 14, 2025

·

Updated May 20, 2026

296 views

8

4.3 (11)

A state machine is the only correct way to parse CSV. The two states are 'in quotes' and 'not in quotes', and every character either advances state or appends to the current field. The trickiest case is the doubled-quote escape ("" inside a quoted field becomes a literal "), which a regex-based parser usually botches. I deliberately do NOT support backslash escapes because RFC 4180 does not, and supporting both produces ambiguous parses. The CRLF handling at the bottom is required for any CSV that came from a Windows tool or from Excel.