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

329
Visualizações
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 Respostas
Responde à pergunta

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 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