Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Title Case

Is there a built in way with jQuery to "title case" a string? So given something like bob smith, it turns into "Bob Smith"?

like image 750
AnApprentice Avatar asked Feb 23 '11 02:02

AnApprentice


1 Answers

You don't need jQuery for this; it can be accomplished using the native .replace() method:

function toTitleCase(str) {     return str.replace(/(?:^|\s)\w/g, function(match) {         return match.toUpperCase();     }); }  alert(toTitleCase("foo bar baz")); // alerts "Foo Bar Baz" 
like image 181
Ben Blank Avatar answered Sep 20 '22 14:09

Ben Blank