Quoted strings and CSV fields
Match strings with escaped quotes, see why a near-identical pattern explodes, and know where to stop and use a real CSV parser.
Negated classesAlternationCatastrophic backtracking and ReDoSParsing log lines with named groups
Quoted strings and CSV fields
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- "[^"]*" stops at the first quote it sees, so it splits a string containing an escaped quote. On my test line it returned four matches where there are three strings.
- "(?:[^"\\]|\\.)*" fixes that: each step is either a character that is not a quote or backslash, or a backslash plus the next character. The two choices never overlap.
- Overlapping choices are dangerous. "(?:[^"]|\\.)*" lets a backslash match both branches, and on an unterminated string it took 875 ms at 40 backslashes, growing by about 2.4 times per 2 added.
- The unrolled form "[^"\\]*(?:\\.[^"\\]*)*" is the same language. It was faster on a well-formed million-character string, but slower on unterminated input in V8, so measure.
- For CSV, quotes are escaped by doubling, and fields can contain newlines and commas. A regex handles one record. For files, use a CSV parser and decide what to do with malformed rows.
When the closing quote is not the end
"[^"]*" was the workhorse of earlier lessons: a quote, anything but a quote, a quote. It works until the content contains a quote. Consider a string with an escaped one:
say "hello" and "she said \"hi\" twice" and "back\\" end
The intended strings are hello, she said \"hi\" twice and back\\. Here is what four patterns returned in Node 26:
"[^"]*" ["hello"] ["she said \"] [" twice"] ["back\\"]
".*?" the same four fragments
"(?:[^"\\]|\\.)*" ["hello"] ["she said \"hi\" twice"] ["back\\"]
"[^"\\]*(?:\\.[^"\\]*)*" the same three strings
The first two read the escaped quote as the end of the string, so the second string is cut into two broken matches, "she said \" and " twice". The last two are right. JSON.parse on each of those three matches returned hello, she said "hi" twice and back\, so the pattern captured what a JSON string literal would.
How the escape-aware pattern thinks
Read "(?:[^"\\]|\\.)*" as a quote, then repeat: either one character that is not a quote or backslash, or a backslash followed by any one character; then a closing quote. A backslash always consumes the character after it, so \" can never act as the end, and \\ is a complete escaped backslash. That is why "back\\" ends where it should.
The two alternatives share no character: a backslash can only enter through the second branch. So there is exactly one way to parse any string, and the engine never has to guess.
The one-character edit that explodes
Remove the backslash from the first class and you get "(?:[^"]|\\.)*". It looks harmless and it works on good input. But now a backslash matches both branches: as a plain character via [^"] and as the start of an escape via \\.. On an unterminated string of backslashes it takes exponentially many routes. Quote plus n backslashes and no closing quote, in Node 26:
n=32 18 ms
n=34 49 ms
n=36 130 ms
n=38 306 ms
n=40 875 ms
About 2.4 times per two characters added. The safe version on the same input took 0.01 ms at 5,000 backslashes. This is the overlapping-alternatives shape from the ReDoS lesson, in a pattern that people write by hand.
Unrolling the loop
The next form is known as “unrolling the loop” (the name is from Jeffrey Friedl’s Mastering Regular Expressions, from memory): "[^"\\]*(?:\\.[^"\\]*)*". It matches the same strings, but instead of one step per character it takes a run of ordinary characters at once, then an escape, then another run. I compared the two on a well-formed string of about a million characters and on unterminated input:
well-formed, 1,050,002 chars alt 0.92 ms unrolled 0.59 ms
unterminated, 28,001 chars alt 76.6 ms unrolled 151.5 ms (unanchored)
unterminated, anchored with ^ alt 0.1 ms
The unrolled form won on good input and lost on the bad input in V8, so the folklore that it is always faster did not hold in this one engine. Notice the third row: the unanchored failures are quadratic, because every escaped quote in the string is another place to start. Anchoring with ^ made it trivial. Choose by measurement for your engine and your data.
CSV fields
CSV (RFC 4180) quotes a field with ", and escapes a quote inside by doubling it: "say ""hi""". A quoted field may contain commas and newlines. The pattern for a quoted field is the same idea with a different escape: "(?:[^"]|"")*". For one record, a sticky regex that matches a field plus its delimiter, in a loop, gave this:
a,b,c [a, b, c]
a,"b,c",d [a, b,c, d]
a,"say ""hi""",d [a, say "hi", d]
a,,c [a, "", c]
,, ["", "", ""]
a,"multi\nline",d [a, multi\nline, d]
a,b"c,d malformed (stray quote)
a,"b malformed (unterminated)
I got that after my first attempt at the loop mishandled ,, and silently returned wrong fields for the stray-quote rows, which is the point: this is easy to get subtly wrong. The naive 'a,"b,c",d'.split(",") returned ["a", "\"b", "c\"", "d"].
Where to stop
Use a CSV parser for files. Fields with newlines mean that splitting the file by line breaks a record in two, and a regex over one line cannot see it. Python’s csv module parsed the multi-line row correctly, and on the malformed rows it showed the policy question: by default a,b"c,d became ['a', 'b"c', 'd'] and the unterminated a,"b became ['a', 'b'], but with strict=True the second raised unexpected end of data. A regex gives you whichever answer your pattern happens to produce. A parser makes the choice an option.
About the video
“CSV Parsing with Regex: Capabilities & Limitations for Beginners” by CodeLucky was published 6 September 2025 and runs 5 minutes 20 seconds. The description says it covers what CSV is, a simple regex approach, handling quoted fields, a Python implementation, and the limitations (escaped quotes, multiline records, complex structures) with csv as the better alternative. The chapters are in that order. I couldn’t read a transcript, so I can’t judge its accuracy, and it is a beginner-level overview that does not cover backslash-escaped strings, unrolled loops or the exponential variant. I have not watched other videos from this channel. Treat it as an introduction, not a reference. English per its title and description.
What I did not run
Other CSV dialects (tab or semicolon separated, \ as the escape character), the RFC’s rules for CRLF handling, streaming parsers, and PCRE or Python timing on the exponential variant. The “unrolling the loop” name is from Friedl’s book Mastering Regular Expressions as I remember it, not checked.
Next
Not everything needs a parser. Everyday tidying such as trimming, case conversion and slugs is where a single replace shines: cleaning and reshaping text.
Check yourself
Why does /"[^"]*"/ fail on "she said \"hi\" twice"?
It treats the first quote after the opening one as the end, even when a backslash escaped it. It returned two fragments of that string, `"she said \"` and `" twice"`, instead of the whole thing. You need a pattern that treats backslash plus any character as one unit.
What are the two alternatives in "(?:[^"\\]|\\.)*" and why must they not overlap?
One matches any character except quote and backslash, and the other matches a backslash and the next character. Because a backslash can only match the second, there is exactly one way to match any string, so a failure is found quickly. Overlap creates many ways, and failures take exponential time.
How is a quote escaped inside a quoted CSV field, and how is that different from a string literal?
CSV doubles it: "say ""hi""". A string literal usually puts a backslash before it. The regex changes accordingly: (?:[^"]|"")* for CSV, and the backslash form for literals.
What are two reasons to stop using a regex and use a CSV parser?
Fields can contain newlines, so a line-by-line approach splits a record in two. And you must pick a policy for malformed input such as a stray quote inside an unquoted field, which parsers expose as options and a pattern leaves to chance.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.