Greetings, I want to capitalize each word in a script, for this I have came up with a such method:
//Word Capitalization
function wordToUpper(val) {
newVal = '';
val = val.toLowerCase().split(' ');
for(var c=0; c < val.length; c++) {
newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
}
return newVal;
}
Now it works for regular words starting after and emtpy char ” “.
However I also want to make sure it fails for such strings:
wordToUpper('hello my name is Hellnar.it doesnt work.') -> Hello My Name Is Hellnar.it Doesnt Work. “it” had to be capital.
wordToUpper('hello my name is (hellnar).') -> Hello My Name Is (hellnar). “Hellnar” had to capital.
Regards
Note: Please not the css classic text-transform: capitalize; solution as this data will be used for form posting.
The following will handle all Unicode letters, unlike any solution using
\w,\bor[a-z]etc. Admittedly the regular expression is rather large. Character lists taken from Steve Levithan’s excellent XRegExp library.