Really, pretty much what the title says.
Say you have this string:
var theString = \"a=b=c=d\";
Now, when you run theString
const theString = "a=b=c=d";
const [first, ...rest] = theString.split("=");
const second = rest.join("=")
console.log(first, second)
If you are using ECMA 2015, you just need 2 lines.
This my implementation:
String.prototype.splitRemainder = function(delim, count) {
if (typeof delim !== 'string') {
return this.split();
}
if (typeof count !== 'number') {
return this.split(delim);
}
if (count < 2) {
return this.split(delim);
}
count--;
const parts = this.split(delim, count);
const remainder = this.slice(parts.join('').length + count);
if (remainder.length > 0) {
parts.push(remainder);
}
return parts;
}
console.log("dasd asds asds asdasd asdasdas".splitRemainder(" ", 4));
console.log("hello-to-you-too".splitRemainder("-",2));
Note that it is not the most efficient way to implement it. So if you're looking for the most efficient solution this is not it.