Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a CSV-like string with quoted fields supporting escaped quotes ("") and unquoted fields, ensuring proper comma separation and no trailing commas.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
( Start of a capturing group (group 1) Start of group
[^,"\r\n]+ Character class: one or more characters that are not commas, double quotes, carriage returns, or line feeds (unquoted field) abc,123,field
| Alternation: OR operator between two patterns Separates unquoted and quoted field patterns
" Literal double quote: starts a quoted field Start of quoted field
([^"]|""|" )* Group (group 2) matching zero or more occurrences of: non-quote characters, escaped quotes ("), or space followed by quote (for trailing space handling) content, "escaped" quote, trailing space
) End of capturing group (group 1) End of group
( Start of a non-capturing group (outer group for repetition) Start of repetition group
, Literal comma: separates CSV fields Comma separator
( Start of a capturing group (group 3) Start of group
[^,"\r\n]+ Character class: one or more characters that are not commas, double quotes, carriage returns, or line feeds (unquoted field) abc,123,field
| Alternation: OR operator Separates unquoted and quoted field patterns
" Literal double quote: starts a quoted field Start of quoted field
([^"]|""|" )* Group (group 4) matching zero or more occurrences of: non-quote characters, escaped quotes ("), or space followed by quote content, "escaped" quote, trailing space
) End of capturing group (group 3) End of group
) End of non-capturing group (outer group for repetition) End of repetition group
* Quantifier: zero or more occurrences of the preceding group Allows multiple fields separated by commas
$ Anchor: asserts position at end of the string End of string

Matches

  • abc,123,field
  • "quoted field",unquoted, "field with ""quote"""
  • a,"b c",d
  • ""
  • " "
  • field1,field2,field3

Does not match

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

Common pitfalls

  • Unquoted fields containing commas or newlines will not match
  • Escaped quotes must be exactly two double quotes ("") inside quoted fields
  • Trailing spaces after quotes in quoted fields are not preserved unless explicitly included in the pattern
  • The pattern does not validate CSV content beyond structure (e.g., no numeric checks)
  • Anchors (^ and $) ensure the entire string matches; partial matches will fail

Performance notes

The pattern has nested quantifiers (*) which can lead to catastrophic backtracking for malformed input (e.g., long sequences of quotes or commas). For large inputs, consider a more efficient parser. The alternation and nested groups increase complexity, so performance degrades with input size.

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 allow " " (space followed by quote) in quoted fields?

This handles trailing spaces in quoted fields, which are sometimes used in CSV formats to preserve spaces after the closing quote.

Can this pattern handle multi-line CSV fields?

No. The pattern explicitly excludes carriage returns (\r) and line feeds (\n) from unquoted fields, and quoted fields cannot contain newlines. For multi-line CSV, a different approach is needed.

How does the pattern handle empty fields?

Empty fields are matched by the quantifier * (zero occurrences) in either the unquoted or quoted pattern. For example, "a,,b" matches with an empty second field.

Why use (,...) instead of just ,... in the repetition group?

The capturing group (group 3) is used to capture each comma-separated field for potential extraction, though the outer group is non-capturing.

Does this pattern work for all CSV edge cases?

No. It handles basic CSV with quoted fields and escaped quotes but does not account for all CSV specifications (e.g., different quote characters, delimiters, or encoding). For robust CSV parsing, use a dedicated library.