Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

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

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'abc,def' (matches)
( Start of a capturing group (group 1) ...
[^",\r\n]+ Character class: one or more characters that are not double quotes, carriage returns, or newlines abc, 123, or xYz
| Alternation: OR operator between two patterns ...
" Literal double quote character Start of quoted field like "abc"
([^"]|"")* Group: zero or more occurrences of either a non-quote character or a pair of double quotes (escaped quote) abc, a""bc, or ""
" Literal double quote character End of quoted field like "abc"
) End of capturing group (group 1) ...
( Start of a non-capturing group (for repetition) ...
, Literal comma character Separator between fields like in 'abc,def'
( Start of a capturing group (group 2, but unused) ...
[^",\r\n]+ Character class: one or more characters that are not double quotes, carriage returns, or newlines abc, 123, or xYz
| Alternation: OR operator ...
" Literal double quote character Start of quoted field like "abc"
([^"]|"")* Group: zero or more occurrences of either a non-quote character or a pair of double quotes (escaped quote) abc, a""bc, or ""
" Literal double quote character End of quoted field like "abc"
) End of capturing group (group 2, but unused) ...
) End of non-capturing group ...
* Quantifier: zero or more occurrences of the preceding group Matches 'abc,def' or 'abc'
$ Anchor: asserts position at end of the string End of 'abc,def' (matches)

Matches

  • abc,def,ghi
  • 123,"a""b",456
  • "quoted field",unquoted
  • simple
  • ""
  • a,"b,c",d
  • field1,field2,field3
  • 1,"2,3",4

Does not match

  • abc,"def
  • "ghi",jkl,
  • abc,"def""
  • abc,def
  • abc,"def"ghi
  • ,abc
  • abc,,def
  • "abc"def"

Common pitfalls

  • Fails to handle escaped quotes outside of quoted fields (e.g., abc"def).
  • Does not account for spaces around commas (e.g., 'abc, def').
  • May incorrectly match strings with trailing commas if not anchored properly.
  • Does not validate CSV rules like balanced quotes in quoted fields.
  • Fails on fields containing literal carriage returns or newlines.

Performance notes

The pattern has nested quantifiers (especially the * inside the quoted field pattern) which can lead to catastrophic backtracking for malformed input (e.g., long strings of quotes). Always validate input length or use possessive quantifiers if possible. The ^ and $ anchors ensure full-string matching, which is correct for CSV validation.

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 is the group ([^"]|"")* used for quoted fields?

This group matches either a non-quote character or a pair of double quotes (to handle escaped quotes). The * quantifier allows zero or more such characters, enabling the pattern to match quoted fields with internal escaped quotes (e.g., 'a""b').

Why are there unused capturing groups (group 2)?

The pattern includes capturing groups for the quoted field content (e.g., ([^"]|"")*) to allow extraction of the field's value, even though the group itself is not used in the overall pattern logic.

Does this pattern handle all CSV edge cases?

No. It does not handle spaces around commas, escaped commas, or fields containing literal newlines. For full CSV compliance, consider a dedicated CSV parser.

Can this pattern be optimized?

Yes. Replace the * quantifier with *? (non-greedy) or use possessive quantifiers (e.g., *+) if supported, to reduce backtracking. Also, consider simplifying the pattern if only validation (not extraction) is needed.