prendre un regard sur ucwords from php.js - cela semble être un peu ce que vous cherchez. Au fond, il est:
function ucwords (str) {
return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
return $1.toUpperCase();
});
}
Notez que THIS IS A TEST
retournera THIS IS A TEST
donc vous devrez l'utiliser comme ceci:
var oldstring = "THIS IS A TEST";
var newstring = ucwords(oldstring.toLowerCase());
ou modifier la fonction un peu:
function ucwords (str) {
str = (str + '').toLowerCase();
return str.replace(/^([a-z])|\s+([a-z])/g, function ($1) {
return $1.toUpperCase();
});
}
var oldstring = "THIS IS A TEST";
var newstring = ucwords(oldstring); // This Is A Test
http://stackoverflow.com/questions/1026069/capitalize-the-first-letter- of-string-in-javascript –
Basculer en minuscules, couper en mots individuels en divisant par des espaces, toUpperCase() sur le premier caractère de chaque mot. – bdares
@ reader_1000: cette question concerne la mise en majuscule du premier caractère d'une chaîne alors que cela concerne le premier caractère de chaque mot (ce qui est beaucoup plus compliqué et nécessite une solution différente) – oezi