RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex to split a comma-separated list

/\s*,\s*/g

As a split pattern this discards the whitespace around each comma, so 'a , b' yields clean values. It is a list splitter, not a CSV parser: quoted fields containing commas need a real CSV reader, and every language ships one.

What each part does

\s*any whitespace before the comma
,the separator itself
\s*any whitespace after it

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
a, bmatches,
a ,bmatches ,
a , bmatches ,
abno match
a;bno match

The mistake to avoid

Using it on real CSV. The line "Smith, John",42 splits into three fields instead of two, and the bug surfaces months later on one customer's export.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers