javascript regex split produces too many items

That’s because split does also push capturing groups to the result array:

If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array.

The space between a and b was matched by the whitespace, so the capturing group was undefined. The comma between b and c was matched by the group, so it became the fourth item of your array.

To solve the issue, just remove the capturing group:

var answers = s.split(/\s*,\s*|\s+/);

If you had a more complex expression where you needed grouping, you could make it non-capturing like this:

var answers = s.split(/(?:\s*,\s*)|\s+/);

Leave a Comment