Shaun Inman’s Widon’t Ported to Javascript Posted on March 20th, 2007 by

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

Contact Us

Phone: 507-933-6111
Email: helpline@gustavus.edu
Web: https://gustavus.edu/gts
Blog: https://gts.blog.gustavus.edu
Remote Support: https://sos.gac.edu
System Status: https://gustavus.freshstatus.io

Sign up for our newsletter.

Receive a daily digest anytime we post something new.

We don’t spam! Unsubscribe at any time!

 


One Comment