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

347
Visualizações
How can i trim just specific characters from my string?

I have the following array

const re = '    ';
var arr  = ['\t', '\n3\t3', '\n\t', '\n3\t3', '\n2\t', '\n\t2', '\n']

i need to trim the \t and \n characters from strings. So when i try

for(let i = 0;i < arr.length;i++) {
let row = arr[i].split(re);
console.log(row);
}

i get

['', '']
['\n3', '3']
['\n', '']
['\n3', '3']
['\n2', '']
['\n', '2']
['\n']

i can't find a way to remove the \n charactes here when i get to this point so when i have just \n as element then it should be replaced with '' - empty string.

If the character includes other things inside for example

\n2

so \n before or after a number then i shold get just the number and to have just 2 inside

How can i replace this \n cahracters

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use replace() function of string combined with map() function of array. It will look like this

arr.map(c => c.replace(/(\n|\t)/gi, ''))

Output will be:

[ '', '33', '', '33', '2', '2', '' ]

If you don't want to see empty strings in array you can just filter them with .filter(Boolean)

arr.map(c => c.replace(/(\n|\t)/gi, '')).filter(Boolean)

Output will be:

[ '33', '33', '2', '2' ]

about 4 years ago · Juan Pablo Isaza Relatório

0

Reference: Here

It is not good practice to do with split method. You can use replace method instead.

Here is example what you want to do:

var someText = "Here's some text.\n It has some line breaks that will be removed \r using Javascript.\r\n";

someText = someText.replace(/(\r\n|\n|\r)/gm,"");

console.log(someText);

You can edit your replace method what you want to use in it.

Note: "/(\r\n|\n|\r)/gm" this is a regex.

about 4 years ago · Juan Pablo Isaza Relatório

0

You may do a regex replacement on [\t\n]+ to remove these characters:

var arr = ['\t', '\n3\t3', '\n\t', '\n3\t3', '\n2\t', '\n\t2', '\n'];
var output = arr.map(x => x.replace(/[\t\n]+/g, ""));
console.log(output);

Note that if there could be other whitespace characters which you also want to remove, just do a regex replacement on \s+.

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