Created
January 15, 2013 19:45
-
-
Save rickycheers/4541395 to your computer and use it in GitHub Desktop.
ucwords in JavaScript - Equivalent to PHP's ucwords() Returns a string with the first letter in upper case of each word.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}; |
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();
...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para mayuscula en cada palabra:
Para mayuscula en la primera letra: