Title Capitalization in JavaScript

John Resig has written a good JavaScript port of the excellent Perl script written by John Gruber that provides pretty capitalization of titles. It is amazing how well the script works for capitalizing words.

Below is an excerpt from the post.

The excellent John Gruber recently released a Perl script which is capable of providing pretty capitalization of titles (generally most useful for posting links or blog posts).

The code handles a number of edge cases, as outlined by Gruber:

  • It knows about small words that should not be capitalized. Not all style guides use the same list of words ? for example, many lowercase with, but I do not. The list of words is easily modified to suit your own taste/rules: "a an and as at but by en for if in of on or the to v[.]? via vs[.]?" (The only trickery here is that ?v? and ?vs? include optional dots, expressed in regex syntax.)
  • The script assumes that words with capitalized letters other than the first character are already correctly capitalized. This means it will leave a word like ?iTunes? alone, rather than mangling it into ?ITunes? or, worse, ?Itunes?.
  • It also skips over any words with line dots; ?example.com? and ?del.icio.us? will remain lowercase.
  • It has hard-coded hacks specifically to deal with odd cases I?ve run into, like ?AT&T? and ?Q&A?, both of which contain small words (at and a) which normally should be lowercase.
  • The first and last word of the title are always capitalized, so input such as ?Nothing to be afraid of? will be turned into ?Nothing to Be Afraid Of?.
  • A small word after a colon will be capitalized.

He goes on to provide a full list of edge cases that this script handles.

You can read the full post here.

I can see some uses for this script in applications that would like to take user input and make it into a properly formatted title. This could be handy in a del.icio.us or digg like application.





Digg!Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >

Thoughts for the Week

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/general/thoughts-for-the-week/.
Well another week down, and what a week it has been! Not only has it been busy…     Readmore

WP Advanced Code Editor

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/announcements/wp-advanced-code-editor/.
For those interested, I wrote a plugin for Techlyzer.com that was just released. It’s called     Readmore

Check Out My Other Home: Techlyzer.com

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/general/check-out-my-other-home-techlyzercom/.
Hi all. I’m happy to announce the launch of a new website that I’ll be trying to…     Readmore

Gadget Advisor Blog

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/reviews/gadget-advisor-blog/.
Being a tech-centric blog I’m always on the lookout for other technology related websites. For those with…     Readmore

Updates to Permalinks

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/announcements/updates-to-permalinks/.
After a lot of deliberation I’ve changed the permalink structure of Simply-Basic. When I started the blog…     Readmore

Qwest Web.Help: Stop Hijacking My Browser (How to Opt-Out)

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/posts/1941493.
Earlier today I was surfing the web and mistyped a URL. Rather then getting the customary “The…     Readmore

Change Default File Type and Path for Screen Captures [Mac Tips]

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/posts/1941482.
Mac only - I frequently use Mac’s built in screen capture ability to create the images you…     Readmore

Coming Soon: Comment on Lifehacker Using Your Facebook ID

Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/posts/1941478.
I just noticed that Lifehacker is joining the ranks of sites using the new Facebook Connect,…     Readmore

dealdotcom
Earn $$ with WidgetBucks!