Split a String on 2nd last occurrence of comma in jquery

You can do that by mixing few methods, just like that:

const str = "xy,yz,zx,ab,bc,cd";
const tempArr = str.split(',');
const a = tempArr.slice(0, -2).join(',');
const b = tempArr.slice(-2).join(',');

console.log("a:", a, "b:", b);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top