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
<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)
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With