Shaun Inman’s Widon’t Ported to Javascript

I was working on some pull-quote magic today (more on that later) and needed to de-widow a string in Javascript a la Shaun Inman’s Widon’t. So I ported it to Javascript.

Here’s the magic:

String.prototype.widont = function() {
	return this.replace(/([^\s])\s+([^\s]+)\s*$/, '$1 $2');
} // end widont()

To use this simply call the widont() method on a string:

var myString	= 'Hello world!';
myString	= myString.widont();

Update (March 22, 2007): After chatting with Shaun a bit, I have updated this to match the technique that he uses in the latest version of his plugin (2.1). Thanks Shaun!

Shaun Inman’s Widon’t 2.1


Comments

One response to “Shaun Inman’s Widon’t Ported to Javascript”

Leave a Reply

Your email address will not be published. Required fields are marked *