Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select last word in a container

I was just wondering if there is a way to select the last WORD in a DIV. I don't think there is any obvious way to do this, so are there any work arounds?

I don't mind using CSS or Javascript to achieve this.

Thanks in advance

like image 321
Lee Price Avatar asked Aug 16 '11 08:08

Lee Price


1 Answers

<div> or no <div>, it boils down to basic String manipulation (using the match()) method.

var words = $('#your_div').text().match(/(\w+)/g);
if (words.length) {
    var last_word = words[words.length - 1];
}

We build an array of all words using the match() method, and then get the last one (var last_word = words[words.length - 1];), but only if some words were found (if (words.length)).

like image 156
Matt Avatar answered Sep 29 '22 00:09

Matt