JavaScript equivalent of Python's rsplit
String.prototype.rsplit = function(sep, maxsplit) {
var split = this.split(sep);
return maxsplit ? [ split.slice(0, -maxsplit).join(sep) ].concat(split.slice(-maxsplit)) : split;
}
This one functions more closely to the Python version
"blah,derp,blah,beep".rsplit(",",1) // [ 'blah,derp,blah', 'beep' ]
You can also use JS String functions split + slice
Python:
'a,b,c'.rsplit(',' -1)[0]
will give you 'a,b'
Javascript:
'a,b,c'.split(',').slice(0, -1).join(',')
will also give you 'a,b'
Assuming the semantics of JavaScript split are acceptable use the following
String.prototype.rsplit = function (delimiter, limit) {
delimiter = this.split (delimiter || /s+/);
return limit ? delimiter.splice (-limit) : delimiter;
}