How can I get the last word of a sentence using jQuery?
<strong> Examples:
get the last word of a sentence
sentence
get the last word
word
try it
var lastword = str.split(" ").pop();
This can be done using JavaScript. Why JQuery?
This will be split your string in separate components, then it will pop out from the last element of the array and return it.
split
More details here
alert("get the last word of a sentence".split(" ").pop());
Try it.
var arr = str.split(' '); var length = arr.length; var result = arr[length - 1]; return result;
Or you can try this as well.
var lastitem = str.split(" ").pop();