This function returns last word of your string.
For example, your string = My lucky color will give you the result = color
function get_last_word(str, sep) { var last_word = ''; if (str != '') { sep = ( ! sep) ? '-' : sep; last_word = str.split(sep).splice(-1)[0]; } return last_word; }
Usage:
alert(get_last_word('My lucky color', ' '));
Note: 2nd parameter is an optional. By default it applies ‘-‘ (hyphen).