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

328
Vistas
Remove Space From Start and End From All Lines of TextArea

What I am looking for remove white space from every line start and end. I am trying the code posted on this answer

var lines = $("textarea[name=f01]") .val().split(/\n/);
var texts = [];
for (var i=0; i < lines.length; i++) {
  if (/\S/.test(lines[i])) {
    texts.push($.trim(lines[i]));
  }
}
var n = texts.toString().split(",").join("\n");
$("textarea[name=f01]") .val(n);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<textarea name="f01" id="40459045A">
 आज शिद्दत से 
दिल चाह रहा है,            
बंद आँखें खोलूं तो 
सामने तुम हो !!
</textarea>

But result is like this

Result

I think when , at the line end, its creating new empty line in it. I want result like

आज शिद्दत से
दिल चाह रहा है,
बंद आँखें खोलूं तो
सामने तुम हो !!

Let me know what I am missing in this. Thanks!

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

0

The code you copied assumes that the text will not contain ,. Converting an array to a string is equivalent to array.join(','). Then it's splitting that with .split(',') to get the array elements. Buf if any of the strings contain a comma, this will split it into two lines in the result.

Just use texts.join('\n'). I have no idea why the other answer uses that.

var lines = $("textarea[name=f01]") .val().split(/\n/);
var texts = [];
for (var i=0; i < lines.length; i++) {
  if (/\S/.test(lines[i])) {
    texts.push($.trim(lines[i]));
  }
}
var n = texts.join("\n");
$("textarea[name=f01]") .val(n);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<textarea name="f01" id="40459045A">
 आज शिद्दत से 
दिल चाह रहा है,            
बंद आँखें खोलूं तो 
सामने तुम हो !!
</textarea>

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