Uppercase first letter of variable

后端 未结 23 1426
粉色の甜心
粉色の甜心 2020-11-30 20:55

I have searched over the web can can\'t find anything to help me. I want to make the first letter of each word upper case within a variable.

So far i have tried:

23条回答
  •  天命终不由人
    2020-11-30 21:17

    I have used this code -

    function ucword(str){
        str = str.toLowerCase().replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g, function(replace_latter) { 
            return replace_latter.toUpperCase();
        });  //Can use also /\b[a-z]/g
        return str;  //First letter capital in each word
    }
    
    var uc = ucword("good morning. how are you?");
    alert(uc);
    

提交回复
热议问题