Regex to Match CSV Fields

Match individual CSV fields handling both quoted and unquoted values. Supports escaped double quotes within quoted fields. Free online regex tester.

Regular Expression

/(?:"([^"]*(?:""[^"]*)*)"|([^",\n]*))/g

Token Breakdown

TokenDescription
(?:Start of non-capturing group
"Matches the literal character '"'
(Start of capturing group
[^"]Negated character class — matches any character NOT in "
*Matches the preceding element zero or more times (greedy)
(?:Start of non-capturing group
"Matches the literal character '"'
"Matches the literal character '"'
[^"]Negated character class — matches any character NOT in "
*Matches the preceding element zero or more times (greedy)
)End of group
*Matches the preceding element zero or more times (greedy)
)End of group
"Matches the literal character '"'
|Alternation — matches the expression before OR after the pipe
(Start of capturing group
[^",\n]Negated character class — matches any character NOT in ",\n
*Matches the preceding element zero or more times (greedy)
)End of group
)End of group

Detailed Explanation

This regex matches individual fields in CSV (Comma-Separated Values) data, handling both quoted and unquoted values according to RFC 4180. Here is the token-by-token breakdown:

(?: — Opens a non-capturing group for the two field type alternatives.

" — Matches the opening double quote of a quoted field.

([^"](?:""[^"])) — Capturing group 1 matches the content of a quoted field. [^"] matches zero or more non-quote characters. The inner non-capturing group (?:""[^"]) handles escaped double quotes: "" represents a literal quote within the field, followed by more non-quote characters. This can repeat zero or more times.

" — Matches the closing double quote of the quoted field.

| — Alternation operator separating quoted and unquoted alternatives.

([^",\n]*) — Capturing group 2 matches an unquoted field. It captures zero or more characters that are not double quotes, commas, or newlines. These are the characters that can appear in a simple unquoted CSV field.

) — Closes the non-capturing group.

The g flag enables global matching to find all fields in a CSV line. This pattern correctly handles fields containing commas when they are quoted, fields containing escaped double quotes, empty fields, and simple unquoted fields. It is useful for CSV parsing, data import tools, and spreadsheet processing applications.

Example Test Strings

InputExpected
simpleMatch
"quoted field"Match
"field with ""quotes"""Match
"has, comma"Match
Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(9 matches)

simple "quoted field" "field with ""quotes""" "has, comma"

Matches & Capture Groups

#1simpleindex 0
Group 1:undefined
Group 2:simple
#2index 6
Group 1:undefined
Group 2:
#3"quoted field"index 7
Group 1:quoted field
Group 2:undefined
#4index 21
Group 1:undefined
Group 2:
#5"field with ""quotes"""index 22
Group 1:field with ""quotes""
Group 2:undefined
#6index 45
Group 1:undefined
Group 2:
#7"has, comma"index 46
Group 1:has, comma
Group 2:undefined
#8index 58
Group 1:undefined
Group 2:
#9index 59
Group 1:undefined
Group 2:
Pattern: 36 charsFlags: gMatches: 9

Ctrl+Shift+C to copy regex

Customize this pattern →