Prevent "widows" in HTML heading tags. A widow (in terms of text and headings) means only one word of a title wraps to the next line. The way to prevent widows with just text is by adding a non-breaking space between the last two words of the text instead of a regular space character.

// With JavaScript
var text = text.replace(/\s(?=[^\s]*$)/g, ' ');

// With PHP
$text = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1 $2', $text);