Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a CSV line where fields may be unquoted or quoted (with escaped quotes as ""), and commas are used as delimiters. The entire line 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, double-quote, carriage return, or line feed abc, 123, field1
| Alternation (OR operator) N/A
, Matches a literal comma ,
| Alternation (OR operator) N/A
" Start of a double-quoted string "
([^"]|"")* Non-capturing group matching zero or more occurrences of either: (a) any character except double-quote, or (b) two consecutive double-quotes (escaped quote) abc", """ (escaped quote)
" End of a double-quoted string "
) End of capturing group (Group 1) N/A
( Start of a non-capturing group (repeated) N/A
, Matches a literal comma ,
( Start of a capturing group (Group 3) N/A
[^,"\r\n]+ Character class matching one or more characters that are not comma, double-quote, carriage return, or line feed abc, 123, field1
| Alternation (OR operator) N/A
, Matches a literal comma ,
| Alternation (OR operator) N/A
" Start of a double-quoted string "
([^"]|"")* Non-capturing group matching zero or more occurrences of either: (a) any character except double-quote, or (b) two consecutive double-quotes (escaped quote) abc", """ (escaped quote)
" End of a double-quoted string "
) End of capturing group (Group 3) N/A
) End of non-capturing group (repeated) N/A
* Quantifier: zero or more occurrences of the preceding group N/A
$ Anchor: asserts position at end of the string End of string

Matches

  • abc,123,field1
  • a,"b,c",d
  • """quoted""",unquoted
  • field1,field2,field3
  • a,"",b
  • x,y,z
  • 1,2,3

Does not match

  • abc,123,field1,
  • a,"b,c"d
  • field1,field2 field3
  • a,"b"c,d
  • abc,"def
  • a,,b
  • a,"b"c

Common pitfalls

  • Fails to handle empty fields correctly if not quoted (e.g., 'a,,b' is invalid).
  • Does not account for escaped quotes outside quoted fields (e.g., 'a,"b"c,d' is invalid).
  • May incorrectly match strings with unescaped newlines within quoted fields.
  • Does not validate CSV structure beyond field delimiters (e.g., missing trailing comma).

Performance notes

The pattern is prone to catastrophic backtracking if the input is malformed (e.g., many consecutive commas or unmatched quotes). Always validate input length and structure before applying. Anchors (^ and $) help limit the scope but do not prevent backtracking in the middle of the pattern.

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 [^,]+?

To explicitly exclude carriage returns and line feeds from unquoted fields, ensuring fields do not span multiple lines unless properly quoted.

How does the pattern handle escaped quotes within quoted fields?

By matching two consecutive double-quotes ("") as a single escaped quote within a quoted field, allowing fields like '""hello""' to be interpreted as "hello".

Can this pattern match a CSV with a single field?

Yes, the pattern allows for a single field (e.g., 'abc') as the entire line.

Why is there a capturing group around the comma in the repeated part?

The capturing group (Group 3) is not strictly necessary for matching but may be used for extracting individual fields in code. The outer non-capturing group handles the repetition.