Capitalize //JavaScript Repository


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
//@ [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">

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



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: 34)