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

182
Visualizações
Capitalise first word in a sentence seperated by dots javascript

Using either a for or while loop, but without using Regex, I have to capitalise the first word in a sentence, in a string. Sentences are seperated by dots.

Currently I have the following

<p id="tekst">This is an. Example text. To showcase. What i want</p>

function highlight()
{
var text = document.getElementById("tekst").innerHTML;
//split the above string into an array of strings 
//whenever a blank space is encountered

let arr = text.split(" ");

//loop through each element of the array and capitalize the first letter.

for (var i = 0; i < arr.length; i++) {
    arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);

}

//Join all the elements of the array back into a string 
//using a blankspace as a separator 
const str2 = arr.join(" ");
console.log(str2);
}

What this currently does, is capitalise the first letter in a word. So the text example would be

This Is An. Example Text. To Showcase. What I Want

The desired result is

THIS is an. EXAMPLE text. TO showcase. WHAT i want

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

0

I hope the commented code below is helpful:

function highlight() {
  var text = document.getElementById("tekst").innerHTML;

  let arr = text.split(". ");

  let newSentence = ''

  //loop through each element of the array and capitalize the first letter.

  for (var i = 0; i < arr.length; i++) {
    //splitting words
    const words = arr[i].split(' ')
    //getting the first word and capitalise it
    words[0] = words[0].toUpperCase()
    //removing the first word from the array and adding the rest of the words to newSentence and adding '. ' to the end of sentence
    newSentence += words.join(' ') + '. '
  }

  //trim the sentence to remove any space in the end
  newSentence = newSentence.trim()

  console.log(newSentence);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You have to split on . first than on (space) this way you get a array than only loop through

function highlight() {
  var text = document.getElementById("tekst").innerHTML;
  //split the above string into an array of strings 
  //whenever a blank space is encountered along with full stop('.')
  let arr = text.split(". ");
  let arr2;
  let fullSentence = '';
  
  //loop through each element of the array and capitalize the first letter.
  for (var i = 0; i < arr.length; i++) {
    arr2 = arr[i].split(" ");
    arr2[0] = arr2[0].toUpperCase();
    
    //get all values from array2 and than join them with a space and end them with '.' . As this is looping so value will be saved in fullSentence and with `+` sign each subsequent value will be joined with previous one
    fullSentence += arr2.join(' ') + '. '
  }
  console.log(fullSentence);
}
highlight();// for automatic run of function
<p id="tekst">This is an. Example text. To showcase. What i want</p>

For loop is always a pain for me so taken help from @Elson Ramos

about 4 years ago · Juan Pablo Isaza Relatório

0

How about something like this ?

function highlight() {
    var text = "This is an. Example text. To showcase. What i want";
    //split the above string into an array of strings 
    //whenever a blank space is encountered

    let arr = text.split(".");

    //loop through each element of the array and capitalize the first letter.

    arr.forEach((string, index) => {
        let sep = string.trim().split(" ");
        sep[0] = sep[0].toUpperCase()
        arr[index] = sep.join(" ");
    });

    //Join all the elements of the array back into a string 
    //using a blankspace as a separator 
    const str2 = arr.join(". ");
    console.log(str2);
}
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