select the last word of the sentence and make it bold

1

I have another problem with the string functions, if I try to convert the text in bold of all the strings that are in a pair index of my array I do not have any problem, the code for that is the following

lineas.map(function(value, indice){
 lineas[indice] = lineas[indice].replace(lineas[indice], "<strong>"+lineas[indice]+"</strong>"))
})

transforms all the text in bold, however if I try to put in bold only the last words of each sentence, I receive bad results

<p id="poesia">
LA SCARPETTA
Mi sa che ieri sera eri di fretta
guarda che ti sei persa la scarpetta.
io stamattina l’ho trovata
con la carrozza ieri sei scappata.
E’ stato bello insieme a te danzare
il Cuore tu mi hai fatto sussultare,
</p>
<button onclick="iterare()">iterar</button>
<script type="text/javascript">
  texto = document.getElementById('poesia').textContent;


  lineas = texto.split(/\n/); // division por saltos de linea
 /*
    ["", "LA SCARPETTA", "Mi sa che ieri sera eri di fretta", "guarda che ti sei persa la scarpetta.", "io stamattina l’ho trovata", "con la carrozza ieri sei scappata.", "E’ stato bello insieme a te danzare", "il Cuore tu mi hai fatto sussultare,", ""]
  */ 


  lineas[2] = lineas[2].replace(lineas[2].slice(0, -3), "<strong>"+lineas[2].slice(-3)+"</strong>");
  // "<strong>tta</strong>tta"

  /* resultado esperado
  mi sa che ieri sera eri di </strong>fretta</strong> */



</script>

I also do not know why two empty quotes are generated at the beginning and end of the array

    
asked by steven 28.04.2017 в 04:05
source

3 answers

2

To change the last word of each sentence of the Poetry, I think to realize a split to the element and accessing the last value with pop() to then apply the slice from the position 0 to the position total minus (-) the length of the element obtained with pop() , so it will be concatenated by adding the bold type ( <b> )

function iterare(){
  texto = document.getElementById('poesia').textContent;
  lineas = texto.split(/\n/);
  lineas.map(function(value, indice){
  let last =  value.split(" ").pop();
  lineas[indice]= lineas[indice].slice(0, lineas[indice].length-last.length)+"<b>"+last+ "</b>";
  });
   document.getElementById('poesia').innerHTML = lineas.join("<br>");	
 }
<p id="poesia">
LA SCARPETTA
Mi sa che ieri sera eri di fretta
guarda che ti sei persa la scarpetta.
io stamattina l’ho trovata
con la carrozza ieri sei scappata.
E’ stato bello insieme a te danzare
il Cuore tu mi hai fatto sussultare,
</p>
<button onclick="iterare()">iterar</button>
    
answered by 28.04.2017 в 04:46
1

First you create an arrays by dividing the string lines [2] into words Words = lineas[2].split(" ") , then remove the last word from the arrays lastWord = Words.pop() (this in turn returns the deleted value) and finally you reset the string with Words.join(" ") ;

  texto = document.getElementById('poesia').textContent;


  lineas = texto.split(/\n/); // division por saltos de linea
 /*
    ["", "LA SCARPETTA", "Mi sa che ieri sera eri di fretta", "guarda che ti sei persa la scarpetta.", "io stamattina l’ho trovata", "con la carrozza ieri sei scappata.", "E’ stato bello insieme a te danzare", "il Cuore tu mi hai fatto sussultare,", ""]
  */ 

  Words = lineas[2].split(" ");
  lastWord = Words.pop();
  lineas[2] =  lineas[2].replace(lineas[2], Words.join(" ")+" <strong>"+lastWord+"</strong>");
  // "<strong>tta</strong>tta"

  /* resultado esperado
  mi sa che ieri sera eri di </strong>fretta</strong> */
  console.log(lineas[2]);
<pre id="poesia">
LA SCARPETTA
Mi sa che ieri sera eri di fretta
guarda che ti sei persa la scarpetta.
io stamattina l’ho trovata
con la carrozza ieri sei scappata.
E’ stato bello insieme a te danzare
il Cuore tu mi hai fatto sussultare,
</pre>
<button onclick="iterare()">iterar</button>
    
answered by 28.04.2017 в 04:51
0

try the following script, first we separate the lines in an array, then each line is separated by words to add the tag " strong " to the last word with " palabras.length- 1 "

texto = document.getElementById('poesia').textContent;
var lineas = texto.split(/\n/);
var linea;
for (var i = 0, i < lineas.length; i++) {
    linea = lineas[i];
    palabras = texto.split(" ");
    palabras[palabras.length-1]="<strong>"+palabras[palabras.length-1]+"</strong>"
}
document.getElementById('poesia').innerHTML = lineas.join("<br>");
    
answered by 28.04.2017 в 19:34