Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^([^,"\r\n]+|"([^"]|""|" )*")(,"([^"]|""|" )*"|[^,"\r\n]+)*$

Matches a single CSV line where fields may be unquoted or quoted (with support for escaped quotes via double quotes). The line must not contain commas or newlines within unquoted fields.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
( Start of a capturing group (Group 1) Group 1 begins
[^,"\r\n]+ Character class: one or more characters that are not comma, double quote, carriage return, or newline abc, 123, field
| Alternation (OR operator) Either this or the next pattern
" Literal double quote (start of quoted field) "
([^"]|""|" )* Non-capturing group (Group 2) repeated zero or more times: matches either a non-quote character, an escaped quote (two quotes), or a space followed by a quote text, "", " "
) End of Group 2 End of quoted field content
" Literal double quote (end of quoted field) "
) End of Group 1 (first field) End of first field
( Start of a non-capturing group (repeated zero or more times for subsequent fields) Start of repeated field group
, Literal comma (field separator) ,
" Literal double quote (start of quoted field in repetition) "
([^"]|""|" )* Non-capturing group (Group 3) repeated zero or more times: same as Group 2 text, "", " "
) End of Group 3 End of quoted field content in repetition
" Literal double quote (end of quoted field in repetition) "
| Alternation (OR operator) Either this or the next pattern
[^,"\r\n]+ Character class: one or more non-comma, non-quote, non-newline characters field
) End of the non-capturing group (repeated fields) End of repeated field
* Quantifier: zero or more occurrences of the preceding group Zero or more fields after the first
$ Anchor: asserts position at end of the string End of string

Matches

  • abc,def,ghi
  • "a,b",c,"d"
  • """
  • field1,"field, with, commas",field3
  • " " , "" , "a"
  • a,"b""c",d

Does not match

  • abc,"def ghi"
  • abc,"def"ghi"
  • abc,def,
  • ,abc,def
  • abc,,def
  • "abc
  • abc"
  • a,b c,d

Common pitfalls

  • Unquoted fields containing commas or newlines will not match
  • Escaped quotes must be represented as two consecutive quotes (e.g., "" inside a quoted field), not a backslash-escaped quote
  • The pattern does not validate CSV structure beyond field separation (e.g., it allows empty fields like ,,)
  • Newlines within fields are not allowed even in quoted fields (due to \r\n exclusion)
  • The pattern may fail to match if the string ends with a comma (e.g., abc,def,)

Performance notes

The pattern is prone to catastrophic backtracking if the input contains many consecutive commas or malformed quoted fields (e.g., "a"""""""""""""""""b"). Anchoring (^ and $) helps limit the scope of matching. For large inputs, consider a streaming CSV parser instead.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why does the pattern use [^,"\r\n]+ instead of [^,]+ for unquoted fields?

To explicitly exclude double quotes and newlines (\r\n) from unquoted fields, ensuring quoted fields are properly handled.

How does the pattern handle escaped quotes inside quoted fields?

By matching two consecutive quotes ("") as a single escaped quote within a quoted field (e.g., "a""b" matches a"b).

Why is there a space in the pattern """" | " "? What does it match?

The space in " " matches a space followed by a quote (e.g., " a" matches a field containing a space and then a quote). This is likely a typo or edge case; it may not be intentional for standard CSV parsing.

Can this pattern match multi-line CSV records?

No. The pattern explicitly excludes \r\n from unquoted fields and does not account for line breaks within fields. It is designed for single-line CSV records only.

How would I modify this pattern to allow empty fields?

Replace all + quantifiers with * to allow zero-length matches (e.g., [^,"\r\n]* instead of [^,"\r\n]+). Note that this may introduce ambiguity in parsing.