The problem with that regular expression is the \b
. It is defined with a reference to word-characters (\w
) which do not include accented characters. So the ã in your example is a word boundary. You can read more about character classes and assertions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
May I suggest another approach without regular expressions?
'rua são luiz'
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
This is just my personal opinion: Regular expressions can quickly become very hard to understand and debug. Sometimes a little more verbose code might be the more maintainable solution.