I've looked all over for how to capitalize the first character of every word of a string, but nothing helped me. I need to set an entered string to a heading capital character lower case one . I've tried this:
function titleCase(str) {
//converting the giving string into array
str =str.split(" ");
//iterating over all elem.s in the array
for(var i=0;i<str.length;i++){
//converting each elem. into string
str[i]=str[i].toString();
//converting the first char to upper case &concatenating to the rest chars
str[i]=str[i].toUpperCase(str[i].charAt(0))+ str[i].substring(1);
}
return str;
}
titleCase("I'm a little tea pot");
I'm A Little Tea Potexpected outputfunction titleCase(str) { str = str.split(" "); for (var i = 0; i < str.length; i++) { str[i] = str[i][0].toUpperCase() + str[i].substring(1); } return str.join(' '); } var str = titleCase("I'm a little tea pot");andvar str = "i'm a little tea pot"; str = str.replace(/^[a-z]|\s[a-z]/g, function(m) { return m.toUpperCase(); });