Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
Poner en mayúscula la primera palabra en una oración separada por puntos javascript

Usando un bucle for o while, pero sin usar Regex, tengo que poner en mayúscula la primera palabra de una oración, en una cadena. Las oraciones están separadas por puntos.

Actualmente tengo lo siguiente

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

Lo que esto hace actualmente es poner en mayúscula la primera letra de una palabra. Así que el ejemplo de texto sería

Esto es un. Texto de ejemplo. Para mostrar. Lo que quiero

El resultado deseado es

Esto es un. EJEMPLO texto. Para mostrar. Lo que quiero

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Espero que el código comentado a continuación sea útil:

 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 Denunciar

0

Tienes que dividir en . primero que en (espacio) de esta manera obtienes una matriz que solo recorre

 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 siempre es un dolor para mí, así que tomé la ayuda de @Elson Ramos

about 4 years ago · Juan Pablo Isaza Denunciar

0

Qué tal algo como esto ?

 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda