nicolasLima
nicolasLima

Reputation: 55

Regular Expression to make first letter of each word capital including special characters

Im working on an application in portuguese so my regular expression must contain the following characters (áàâãéèêíïóôõöúçñ) so far i have /(\b[a-z])/g but it treats those special characters as the start of a new word. Im doing this in Javascript

Example:

Input: rua são luiz
Current output: Rua SãO Luiz
Desired Output: Rua São Luiz

Upvotes: 1

Views: 559

Answers (1)

Manuel
Manuel

Reputation: 36

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.

Upvotes: 2

Related Questions