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 double-quoted (with escaped quotes as ""). Fields are separated by commas, and the entire string must start and end properly.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
( Start of a capturing group (Group 1) N/A
[^,"\r\n]+ Character class matching one or more characters that are not comma, quote, carriage return, or newline abc123
| Alternation (OR operator) N/A
" Literal double quote character "
( Start of a non-capturing group (for alternation) N/A
[^"] Character class matching any character except a double quote a
| Alternation (OR operator) N/A
"" Escaped double quote (represents a single quote in the string) ""
) End of non-capturing group N/A
* Quantifier: zero or more repetitions of the preceding group "a""b"
" Literal double quote character "
) End of capturing group (Group 1) N/A
( Start of a non-capturing group (repeated for additional fields) N/A
, Literal comma character ,
( Start of a capturing group (Group 4) N/A
[^,"\r\n]+ Character class matching one or more characters that are not comma, quote, carriage return, or newline xyz789
| Alternation (OR operator) N/A
" Literal double quote character "
( Start of a non-capturing group (for alternation) N/A
[^"] Character class matching any character except a double quote b
| Alternation (OR operator) N/A
"" Escaped double quote (represents a single quote in the string) ""
) End of non-capturing group N/A
* Quantifier: zero or more repetitions of the preceding group "b""c"
" Literal double quote character "
) End of capturing group (Group 4) N/A
) End of non-capturing group (repeated for additional fields) N/A
* Quantifier: zero or more repetitions of the preceding group (allows zero or more fields after the first) ,field2,field3
$ Anchor: asserts position at end of the string End of string

Matches

  • field1,field2,field3
  • field1,"field,with,commas",field3
  • "field with ""quotes""",simple
  • a,b,c
  • ""
  • 123,456,789
  • a,"b""c",d

Does not match

  • field1,field2 field3
  • field1,"unterminated
  • ,field2
  • field1,,field3
  • field1,"field with newline",field3
  • field1,"field"extra
  • field1,"field""
  • field1,"field"""

Common pitfalls

  • Unterminated quoted fields (missing closing quote) will not match.
  • Newlines within fields (even in quoted fields) will cause failure.
  • Empty fields between commas (e.g., 'a,,b') are not matched by this pattern.
  • Trailing commas (e.g., 'a,b,') are not matched.
  • Escaped quotes must be doubled (e.g., '""' for a single quote in a quoted field).
  • The pattern does not allow spaces around commas (e.g., 'a, b' fails).

Performance notes

This pattern has a risk of catastrophic backtracking due to nested quantifiers (especially the * inside the quoted field pattern). For very long strings, consider a more efficient parser. The ^ and $ anchors ensure the entire string is validated, which is good for correctness but may impact performance on large inputs.

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?

The pattern explicitly excludes quotes and newlines (\r\n) in unquoted fields to prevent ambiguity with quoted fields. This ensures that a quote in an unquoted field would not be mistaken for the start of a quoted field.

Can this pattern match a single empty field (e.g., just a comma)?

No. The pattern requires at least one field (the first group is mandatory), and the * quantifier only applies to additional fields. An empty string or a single comma will not match.

How does the pattern handle escaped quotes inside quoted fields?

Escaped quotes are represented as two consecutive double quotes (e.g., '""'). The pattern ("([^"]|"")*") matches a quoted field by allowing any sequence of non-quote characters or doubled quotes inside the quotes.