Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a CSV-like string with quoted fields that may contain escaped quotes, where fields are separated by commas and the entire string is properly structured without 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 field pattern
[^",\r\n]+ Character class matching one or more characters that are not double quotes, carriage returns, or line feeds abc123
| Alternation: OR operator between two patterns Separates quoted and unquoted field patterns
" Literal double quote character Start of quoted field
([^"]|""|" )* Group 2: Matches zero or more occurrences of either a non-quote character, two double quotes (escaped quote), or a space followed by a quote (edge case for quoted spaces) field content with "escaped" or " quoted
" Literal double quote character End of quoted field
) End of capturing group (Group 1) End of field pattern
( Start of a non-capturing group (outer quantifier group) Start of repetition for additional fields
, Literal comma character Field separator
( Start of capturing group (Group 3) for the next field Start of next field pattern
[^",\r\n]+ Character class matching one or more characters that are not double quotes, carriage returns, or line feeds nextfield
| Alternation: OR operator between two patterns Separates quoted and unquoted field patterns
" Literal double quote character Start of quoted next field
([^"]|""|" )* Group 4: Matches zero or more occurrences of either a non-quote character, two double quotes (escaped quote), or a space followed by a quote next field content with "escaped"
" Literal double quote character End of quoted next field
) End of capturing group (Group 3) End of next field pattern
) End of non-capturing group (outer quantifier group) End of repetition for additional fields
* Quantifier: zero or more occurrences of the preceding group Allows zero or more additional fields
$ Anchor: asserts position at end of the string End of string

Matches

  • abc,def,ghi
  • "quoted field",unquoted
  • "field with ""escaped"" quotes",simple
  • " space at end" ,value
  • a,"b c",d

Does not match

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

Common pitfalls

  • Fails to handle fields containing literal backslashes (not part of the pattern's design)
  • Does not support multi-line quoted fields (only single-line fields with \r\n excluded)
  • The pattern for quoted spaces (" ) is unusual and may not match intended cases
  • No validation for balanced quotes within quoted fields
  • Does not handle empty fields between commas (e.g., a,,b) correctly as it requires at least one character in unquoted fields

Performance notes

The pattern has potential for catastrophic backtracking due to nested quantifiers and alternations, especially with long quoted fields containing many escaped quotes. The use of * quantifiers without upper bounds exacerbates this risk. Anchoring at both start (^) and end ($) helps limit the scope but does not eliminate the risk entirely. For large inputs, consider a more constrained pattern or a parser.

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

The pattern includes an unusual alternation for a space followed by a quote (" ) to handle edge cases where a space precedes a quote in a quoted field, which is not standard CSV escaping. Standard CSV would use "" for escaped quotes and not include the space-quote combination.

Does this pattern support CSV files with CRLF line endings?

No, the pattern explicitly excludes carriage returns (\r) and line feeds (\n) from unquoted fields, but it does not handle multi-line quoted fields. The entire string must be a single line.

How can I modify this pattern to handle empty fields between commas?

To handle empty fields, replace the character classes [^",\r\n]+ with [^",\r\n]* (making the + a *). This allows zero or more characters, permitting empty fields like a,,b.