Leo T Abraham
Leo T Abraham

Reputation: 2437

How to get the last word of a sentence using jQuery?

How can I get the last word of a sentence using jQuery?

Examples:

Upvotes: 4

Views: 5193

Answers (3)

Abhijit
Abhijit

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

SK.
SK.

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.

More information here

alert("get the last word of a sentence".split(" ").pop());

Upvotes: 0

user1844933
user1844933

Reputation: 3417

try this

var lastword = str.split(" ").pop();

Upvotes: 9

Related Questions