DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Capitalize //JavaScript Function

11.21.2005
| 30008 views |
  • submit to reddit
        <a href="http://jsfromhell.com/string/capitalize">

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>

usage:

document.write(s = "jonas Raoni a SOARES silva", " => ", s.capitalize());

code:

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/capitalize [v1.0]

String.prototype.capitalize = function(){ //v1.0
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};