This can be done in one line of code:
myString.replace(/(([^\s]+\s+){27}).+/, "$1...");
Or you can make it a function:
function truncateString(s, wordCount) { var expr = new RegExp("(([^\\s]+\\s+){" + wordCount + "}).+"); return s.replace(expr, "$1..."); }
So, to make this work for your code, you can do:
var post = $('div.shortblogpost').text(); // get the text post = postText.replace(/(([^\s]+\s+){27}).+/, "$1..."); // truncate the text $('div.shortblogpost').text(post); // update the post with the truncated text
source share