Regex to leave first letter of full name capitalized, even with special character [duplicate]

0

I have the following variable in $ scope of Angular JS, with an arrow function:

$scope.cad.nome.toLowerCase().replace(/\b\w/g, l => l.toUpperCase());

I need to format your content, which is someone's full name. It is working until the name has some accent or "Ç". He makes the first few letters of each name capitalized, but when he finds an accent, he makes the next letter stay capital letter as well. example:

MaríLia MendonçA

How do I change the regex so this does not happen?

    
asked by anonymous 23.02.2018 / 18:29

2 answers

1

Your question gives you the answer here: Convert each first letter of each word to uppercase .

But I adapted to format names, I will not go into details.

const formataNome = str => {
    return str.toLowerCase().replace(/(?:^|\s)(?!da|de|do)\S/g, l => l.toUpperCase());
};

console.log(formataNome('marília mendonça'))
    
23.02.2018 / 18:58
0

A little overkill .. but solves the problem.

let regex = /^\D/g;

let nomes = [
  "maria do carmo",
  "nome com çedilha",
  "nome com &special",
  "marilia mendonça"
];

nomes
.map(nome => {
  // Essa parte q corrige o nome individualmente.
  return nome.split(" ")
          .map(part => part.replace(regex, l => l.toUpperCase()))
          .join(" ");
})
.forEach(n => console.log(n));
    
23.02.2018 / 18:48