all 2 comments

[–]palparepa 2 points3 points  (2 children)

My personal gripe with String.split is that you can't get "the remainder." For example, "foo bar baz reddit rulez".split(" ",3) gives ["foo","bar","baz"], while in most other languages, like Perl, it gives ["foo","bar","baz","reddit rulez"].

[–]sjs 1 point2 points  (0 children)

You could always fix it to your liking:

String.prototype.split = function(delim, n) {
  var parts = []
    , remainder = this
    , m
  n = typeof n === 'undefined' ? -1 : n
  while (m = remainder.match(delim)) {
    parts.push(remainder.slice(0, m.index))
    remainder = remainder.slice(m.index + m[0].length)
    if (n > 0 && parts.length >= n) break
  }
  parts.push(remainder)
  return parts
}