[Solved] Split and grab text before second hyphen


You can try with String.prototype.slice()

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

and String.prototype.lastIndexOf()

The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.

var str="test-shirt-print";
var res = str.slice(0, str.lastIndexOf('-'));
console.log(res);

You can also use split() to take the first two items and join them:

var str="test-shirt-print";
var res = str.split('-').slice(0,2).join('-');
console.log(res);

solved Split and grab text before second hyphen