Function splitOn

Similar to String.prototype.split except that

  1. limit is interpreted as the maximum number of splits, and the returned array contains limit + 1 entries. Also if limit is given and the number of substrings is greater than the limit, all the remaining substrings are present in the final substring.
  2. If input is an empty string (or containing only whitespace), returns an empty array.
splitOn('foo, bar, baz', RegExp(',\\s*'), 0); // -> ['foo, bar, baz']
splitOn('foo, bar, baz', RegExp(',\\s*'), 1); // -> ['foo', 'bar, baz']
splitOn('foo, bar, baz', RegExp(',\\s*'), 2); // -> ['foo', 'bar', 'baz']
splitOn('foo, bar, baz', RegExp(',\\s*'), 3); // -> ['foo', 'bar', 'baz']
  • Parameters

    • input: string
    • separator: string | RegExp
    • Optionaltrim: boolean

      If true, entries will be trimmed for whitespace after splitting.

    • Optionallimit: number

      If not given or < 0, the string will be split on every occurrence of separator. Otherwise, it will be split on the first limit number of occurrences of separator.

    Returns string[]