0

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

function capitalizeFirstLetter(str) {
  return str.replace(/\w\S*/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.

Thanks.

kukkuz
  • 41,512
  • 6
  • 59
  • 95
Ozan Ertürk
  • 465
  • 5
  • 17
  • Possible duplicate of [Regex - What would be regex for matching foreign characters?](http://stackoverflow.com/questions/3009993/regex-what-would-be-regex-for-matching-foreign-characters) – NicolasMoise Oct 03 '16 at 18:00

1 Answers1

1

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

Jarod Moser
  • 7,022
  • 2
  • 24
  • 52