Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a CSV-like string where each field is either unquoted (no commas, quotes, or newlines) or double-quoted (with escaped quotes as ""). Fields are separated by commas, optional whitespace, and the entire string must not end with a trailing comma.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string
( Start of a non-capturing group (outer alternation)
[^,"\r\n]+ Character class: one or more characters that are not comma, quote, carriage return, or newline abc,123,field
| Alternation: matches either the left or right side of the group
" Literal double quote: starts a quoted field "
([^"]|"")* Group: zero or more occurrences of either a non-quote character or an escaped quote ("") text""more
" Literal double quote: ends a quoted field "
) End of the outer alternation group
(,\s*([^,"\r\n]+|"([^"]|"")*"))* Group: zero or more occurrences of a comma, optional whitespace, and a field (same pattern as first field) , field1, "quoted"
$ Anchor: asserts position at end of the string

Matches

  • abc,123,"quoted,field"
  • "escaped""quote",field2
  • simple
  • a,"b",c
  • field1, field2, "field,3"

Does not match

  • abc,
  • abc,"unterminated
  • abc,"field""
  • abc, field
  • abc,"field
  • field,
  • a,"b",c,

Common pitfalls

  • Failing to escape quotes inside quoted fields (e.g., using " instead of "")
  • Assuming trailing commas are allowed (they are not in this pattern)
  • Ignoring whitespace after commas (pattern allows optional whitespace)
  • Not accounting for escaped quotes in quoted fields (e.g., "" is valid, " is not)

Performance notes

The pattern has nested quantifiers (*) which can lead to catastrophic backtracking for malformed input (e.g., long sequences of quotes). Anchors (^ and $) are correctly placed to prevent partial matches. Consider simplifying or using possessive quantifiers if performance is critical.

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 ("[^"]|"")* instead of ("[^"]*")?

The pattern ("[^"]|"")* correctly handles escaped quotes by treating "" as a single escaped quote character, whereas ("[^"]*") would fail to match fields like "a""b" correctly.

Can this pattern match a single empty field?

No. The pattern requires at least one character in unquoted fields ([^,"\r\n]+) and quoted fields must contain at least one character between quotes ("([^"]|"")*").

Does this pattern allow whitespace around commas?

Yes. The \s* after the comma allows optional whitespace between fields.