Regex Rules in Lodash _.split Explained

The Lodash _.split method divides a string into an array of substrings using a separator and an optional limit. When a regular expression is passed as the separator, _.split delegates pattern evaluation to the ECMAScript RegExp engine via String.prototype.split or RegExp.prototype[Symbol.split]. This guide breaks down the specific regular expression rules, capturing group behaviors, flags, and delimiter mechanics that govern string division in Lodash.

Standard Pattern Matching

When passed a standard regular expression, _.split identifies matches against the pattern and uses them as points of truncation. The characters matching the expression are consumed and omitted from the final array.

_.split('apple1banana2orange3grape', /\d/);
// Output: ['apple', 'banana', 'orange', 'grape']

Capturing Groups Inclusion

If the regular expression contains capturing parentheses (), the captured submatches are spliced directly into the returned array alongside the non-matching chunks. Non-capturing groups (?:) do not output to the array.

// Capturing group: delimiters are preserved in the array
_.split('2023-10-25', /(-)/);
// Output: ['2023', '-', '10', '-', '25']

// Multiple capturing groups
_.split('A:B;C', /(:|;)/);
// Output: ['A', ':', 'B', ';', 'C']

// Non-capturing group: delimiters are discarded
_.split('A:B;C', /(?::|;)/);
// Output: ['A', 'B', 'C']

If a capturing group matches an empty string or fails to match entirely (such as within an alternation), it returns undefined in the output array according to ECMAScript standard rules.

Regex Flags Handling

Regular expression flags change the search mechanics:

_.split('Cat,BAT,mat', /[a-z]at/i);
// Output: ['', ',', ',', '']

Zero-Width and Lookaround Matches

When a regular expression matches an empty string or uses zero-width assertions (lookaheads and lookbehinds), the string is split without consuming surrounding characters.

// Split on capital letters using positive lookahead
_.split('camelCaseWordsHere', /(?=[A-Z])/);
// Output: ['camel', 'Case', 'Words', 'Here']

// Split after hyphens using lookbehind
_.split('step1-step2-step3', /(?<=-)/);
// Output: ['step1-', 'step2-', 'step3']

Empty Regex Delimiters

Passing an empty regular expression /(?:)/ causes the string to split into individual UTF-16 code units, matching native JavaScript behavior:

_.split('abc', /(?:)/);
// Output: ['a', 'b', 'c']

Interaction with the Limit Parameter

The third parameter of _.split(string, separator, limit) restricts the maximum length of the returned array. The regex engine executes matches in order, but evaluation stops once the limit count is reached.

_.split('one:two:three:four', /:/, 2);
// Output: ['one', 'two']

If capturing groups are present, captured values count toward the limit:

_.split('one:two:three', /(:)/, 2);
// Output: ['one', ':']

Lodash Null and Type Coercion

Before regex rules apply, Lodash guards against non-string inputs. If the target value is null or undefined, _.split returns an empty array [] rather than throwing a TypeError as native String.prototype.split does. Non-string primitives passed to the first argument are converted to strings before the regular expression is executed.