piouPiouM/underscore — comment on commit c01011f205423fff430a4cdec1d1369cb6bb3743
File: underscore.js line 1087
By piouPiouM:

This keeps the given separator at the end of each chunk.

The native JavaScript command `split` removes the given separator.
If I want to keep it (like in the Ruby version), I need to use a lookbehind assertion but, because JavaScript does not support them, I use a lookahead assertion in a reversed string.

>>> "foo#bar#baz".split("#")
["foo", "bar", "baz"] // the separator is stripped.
>>> "foo#bar#baz".split(/(?=#)/)
["foo", "#bar", "#baz"] // with a lookahead assertion, each chunk begins with the separator instead of to end with it.

Finally:

>>> _.reverse("foo#bar#baz").split(/(?=#)/)
["zab", "#rab", "#oof"]
>>> _.map(_.reverse("foo#bar#baz").split(/(?=#)/), _.reverse)
["baz", "bar#", "foo#"]
>>> _.map(_.reverse("foo#bar#baz").split(/(?=#)/), _.reverse).reverse()
["foo#", "bar#", "baz"] // Yeah!