Capitalizing First Letter for each word in string. Localization issue

I am using this function to capitalize first letter of words in the string

function capitalizeFirstLetter(str) {
  return str.replace(/wS*/g, function (txt) { 
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); 

It works well for english words but when I try to type my word with non-English first letter, it makes first two letter to uppercase

Input string : “şanlıurfa”
Output string : “ŞAnlıurfa”

I just want to capitalize the first letter.



The regex is what is causing the problem, it isn’t selecting the first character if it is a special character, change it to S+ and it will work as you need. Here is a jsfiddle with it working

Leave a Reply

Your email address will not be published. Required fields are marked *