Regex to make full-name first letter uppercase, whether or not with a special character

Asked

Viewed 1,203 times

0

I have the following variable in the Angular JS $Scope, with an Arrow Function:

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

Which I need to format your content, which is someone’s full name. It’s working until the name has some accent or "Ç". It makes the first letters of each name uppercase, but when it finds an accent, it makes the next letter uppercase as well. example:

Marília Mendonça

How to change the regex so it doesn’t happen?

2 answers

1


Your question is the answer here: Convert every first letter of every word into uppercase.

But I adapted to format names, I won’t go into detail.

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

console.log(formataNome('marília mendonça'))

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));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.