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.
| Input | Result | Matched |
|---|---|---|
| a, b | matches | , |
| a ,b | matches | , |
| a , b | matches | , |
| ab | no match | — |
| a;b | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID