Capitalize //JavaScript Repository

Description

Capitalizes a string, first letter in upper case and the rest in lower case.
Created: 2005.11.20 - Modified 2009.02.06

Code (Download)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/capitalize [rev. #2]

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

Example (Example)

<script type="text/javascript">
//<![CDATA[

var s = "x jonas Raoni SOARES silva ~84";
document.write(('"' + s + '".capitalize() = ').bold(), s.capitalize());

//]]>
</script>

Help

String.capitalize(void): String
Returns the string with the first letter of the words in upper case and the others in lower case.

Rank (Votes: 38)

4.39