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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.