function capitalize(str){
return str.replace( /(^|\s)([a-z])/g ,
function(m,p1,p2){ return p1+p2.toUpperCase();
} );
};
var myString = "i am a boy !"
console.log(capitalize(myString)) //I Am A Boy!
转载于:https://my.oschina.net/AdamHoly/blog/1545199