Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
Capitalize the first character of most words except some exceptions using javascript

I have the following JavaScript regex which capitalizes the first character of most words except some exceptions:

There are two issues with this regex that I have not resolved. The very first letter should always be capitalized. The second letter of every word should be lower case.

Here is an example of how it is working:

function capitalizeFirst(str) {
  return str.replace(/(?!^)\b(?!(?:of|the)\b)([a-z])/g, m => m.toUpperCase());
}


console.log(capitalizeFirst('the fox JUMP off of THE street'));

Where it should be returning this: ('of' and 'the' should be lower case except if on first word). Also it should lower case the rest of the word:

The Fox Jump Off of the Street

I could not find an answer to this specific question during a search.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I was able to figure out the solution. This is an explanation of fix:

  • The toLowerCase was added in the beginning to lower all characters
  • The regex was modified accordingly to handle this:
    enter image description here https://regex101.com/

function capitalizeFirst(str) {
  return str.toLowerCase()
            .replace(/\b(?!(?!^)(?:of|the)\b)([a-z])/g, m => m.toUpperCase());
}

console.log(capitalizeFirst('the fox JUMP off of THE street'));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda