You can try:
- Converting the entire string to lowercase
- Then use replace() method to convert the first letter to convert first letter of each word to upper case
str = "hEllo woRld";
String.prototype.initCap = function () {
return this.toLowerCase().replace(/(?:^|\s)[a-z]/g, function (m) {
return m.toUpperCase();
});
};
console.log(str.initCap());