I have a form with some text inputs to insert First Name
, Last Name
of a person, but I want to change the first letter of each word to a Uppercase and I found this solution:
// The textboxes with this class are modified
$(".toUpper").change(function () {
var str = $(this).val();
str = str.toLowerCase().replace(/\b[a-z]/g, function (letter) {
return letter.toUpperCase();
});
$(this).val(str);
});
and it works, ("hEllO"=>"Hello", "whAts uP" =>"Whats Up")
.
The problem occurs when I try to apply this to an accented word, Example:
"gonzález" = "GonzáLez",
"pérez" = "PéRez"
After an accented word there is a Uppercase letter again.
How can I modify the regular expression to avoid this issue?
hope you can help me!! :)
[a-z]
doesn't matché
. You'll have to be a bit more lenient:And the output:
\b
is a non word boundary (i.e\b
would make a boundary for any any character which doesn't belong to any 1 of[0-9a-zA-Z_]
)So those accented word become the boundary for your word..
Instead use this regex