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