Javascript: Capitalization of each Word in String
问题 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