You have to do it with
function get_last_part(str){
var split = str.split('_');
return split[split.length-1];
}
console.log(get_last_part("ContentPlaceHolderDefault_MainSiteSectionArea_MyPagePlaceHolder_Item4_FavoritAmusementCalender_6_deleteRight_2"));
This way you always get the result, and you don’t have to worry about index issues. This will always return the last part of your string; if it does not _, you will get its first part.
console.log(get_last_part("Content")); // will write "Content" into console
source
share