A jQuery “widon’t” snippet

In the interest of more attractive Internets, here’s a quick little javascript “widon’t” snippet — written in jQuery — that I’ve been using. Now go do something cool with it.

jQuery(function($) {
    $('h1,h2,h3,li,p').each(function() {
        $(this).html($(this).html().replace(/\s([^\s<]+)\s*$/,'&nbsp;$1'));
    });
});

Feel free to replace or augment the selector (h1,h2...p) with any additional elements you’d like to see widon’ted.

I actually use this variant, which lets longer words be widows if they wanna:

jQuery(function($) {
    $('h1,h2,h3,li,p').each(function() {
        $(this).html($(this).html().replace(/\s([^\s<]{0,10})\s*$/,'&nbsp;$1'));
    });
});