Truncate a string in jQuery without cutting off word

If you'd like to display a preview of some text without cutting ending the preview in the middle of a word you can use the following snippet:

jQuery.trim("This is a nice long sentence from which a preview will be created).substring(0, 10).split(" ").slice(0, -1).join(" ") + "..."

This will take the first 10 characters and ensure that a sentence doesn't end with a word being partially cut off. An elipsis (...) is added to the end of the preview.