-
-
Save rickycheers/4541395 to your computer and use it in GitHub Desktop.
String.prototype.ucwords = function() { | |
str = this.toLowerCase(); | |
return str.replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g, | |
function(s){ | |
return s.toUpperCase(); | |
}); | |
}; |
It wont work with accents... like "Úrsula"
It will now....
var str = "bjôrm пô~р да₧иƒоªич üder12 ñƒque αλφ¢";
str = str.toLowerCase().replace(/^[\u00C0-\u1FFF\u2C00-\uD7FF\w]|\s[\u00C0-\u1FFF\u2C00-\uD7FF\w]/g, function(letter) {
return letter.toUpperCase();
});
alert(str);
Para mayuscula en cada palabra:
var lsCadena = "hola MUNDO";
lsCadena = lsCadena.toLowerCase().replace(/\b[a-z]/g, function(letra) {
return letra.toUpperCase();
});
//salida: Hola Mundo
Para mayuscula en la primera letra:
var lsCadena = "hola MUNDO";
lsCadena = lsCadena .toLowerCase();
lsCadena = lsCadena .charAt(0).toUpperCase() + lsCadena .slice(1);
//salida: Hola mundo
Como respuesta a esto tambien si quieres ahorrarte el paso de declarar lsCadena a lowerCase lo puedes hacer en el mismo lsCadena.slice(1).toLowerCase();
Pero tu forma sirve!
I use str.toLowerCase().replace(/(?<= )[^\s]|^./g, a=>a.toUpperCase())
I use
str.toLowerCase().replace(/(?<= )[^\s]|^./g, a=>a.toUpperCase())
Thanks, this worked.
I use
str.toLowerCase().replace(/(?<= )[^\s]|^./g, a=>a.toUpperCase())
Thanks, this worked.
Careful though, that last one doesn't work in Safari. For some reason, it doesn't support lookbehind regex.
So I am now using (' '+str).replace(/ [\w]/g, a => a.toLocaleUpperCase()).trim();
...
Hi,
It's don't work using
a clash of KINGS
, I gotA Clash of Kings
instead ofA Clash Of Kings