Skip to content
JavaScript: Keep delimiter captures out of split results

JavaScript: Keep delimiter captures out of split results

November 19, 2026

A regular expression can split text on several delimiters, but capturing parentheses also insert the matched delimiters into the result. Use a noncapturing group when parentheses only group alternatives and the result should contain fields.

Set up

Tested on Debian 13 Linux with Node.js v24.19.0. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as javascript-split-noncapturing-delimiters.mjs:

import assert from 'node:assert/strict';

const input = 'alpha,beta::gamma';
const captured = input.split(/(,|::)/);
const fields = input.split(/(?:,|::)/);
assert.deepEqual(captured, ['alpha', ',', 'beta', '::', 'gamma']);
assert.deepEqual(fields, ['alpha', 'beta', 'gamma']);
assert.deepEqual('alpha,,::'.split(/(?:,|::)/), ['alpha', '', '', '']);
assert.deepEqual(''.split(/(?:,|::)/), ['']);
assert.deepEqual('plain'.split(/(?:,|::)/), ['plain']);
assert.deepEqual('alpha,beta::gamma'.split(/(?:,|::)/, 2), ['alpha', 'beta']);
assert.equal(input, 'alpha,beta::gamma');
console.log('Capturing separator:', JSON.stringify(captured));
console.log('Noncapturing separator:', JSON.stringify(fields));
console.log('Empty fields:', JSON.stringify('alpha,,::'.split(/(?:,|::)/)));
console.log('Empty input, no delimiter, limit, and source checks: passed');

Run:

node javascript-split-noncapturing-delimiters.mjs

Observed output:

Capturing separator: ["alpha",",","beta","::","gamma"]
Noncapturing separator: ["alpha","beta","gamma"]
Empty fields: ["alpha","","",""]
Empty input, no delimiter, limit, and source checks: passed

Group alternatives without capturing them

The first pattern captures a comma or a double colon. Its result has five elements because both delimiters appear between the three fields. The noncapturing version recognizes the same separators and returns only the three fields.

ECMAScript’s regular-expression split algorithm appends capture results as it builds the array. Captures can be useful when the caller needs to retain separators, but a later column-count check may reject them if it expects only field values.

The delimiters have different lengths, so alternatives express the intended grammar directly. A character class would match individual characters; it would not express a two-character delimiter as a unit. The tested patterns use no zero-length alternatives.

Preserve the empty-field policy

Adjacent and trailing separators produce empty fields. Changing to a noncapturing group does not remove them. The example keeps every empty field so positions remain meaningful; filtering them out would change the record’s shape.

An empty input produces one empty field with this separator, and input without a delimiter produces one unchanged field. The limit assertion shows another boundary: requesting two results discards the remaining text. It does not put the unsplit remainder into the last field.

This small format has no quoting, escapes or embedded-delimiter rules. It is not a CSV parser. Use a parser for the actual data format when delimiters may occur inside quoted values, and validate the resulting fields before using them.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- javascript-split-noncapturing-delimiters.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on