Reputation: 2437
How can I get the last word of a sentence using jQuery?
Examples:
get the last word of a sentence
should return sentence
get the last word
should return word
Upvotes: 4
Views: 5193
Reputation: 252
Try this.
var arr = str.split(' ');
var length = arr.length;
var result = arr[length - 1];
return result;
Or you can try this also.
var lastitem = str.split(" ").pop();
Upvotes: 0
Reputation: 4358
It can be done using JavaScript. Why JQuery?
This will split
your string into the individual components then it will pop off the last element of the array and return it.
alert("get the last word of a sentence".split(" ").pop());
Upvotes: 0