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

132
Visualizações
replaceAll function in javscript doesn't replace all occurrences

I have the following snippet:

const paragraph = '1\t1\t150\t18\t\"Pack of 12 action figures (variety)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n2\t1\t151\t21\t\"Pack of 12 action figures (male)\"\t21\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n3\t1\t152\t18\t\"Pack of 12 action figures (female)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n4\t2\t76\t8\t\"\\\"The ';
const regex = /(?!\B"[^"]*)\t(?![^"]*"\B)/g;
const found = paragraph.replaceAll(regex, ',');

console.log(found);

This snippet should match all of \t, except the ones between quotes, from paragraph and replace them all with comma ,. However, all of them don't get replaced as the text I parse looks like this:

'1,1,150,18,"Pack of 12 action figures (variety)",18,9,5.50,2013-01-02 00:00:00.0000000,True,6,2013-01-02 07:00:00.0000000
2,1,151,21,"Pack of 12 action figures (male)",21,9,5.50,2013-01-02 00:00:00.0000000,True,6,2013-01-02 07:00:00.0000000
3,1,152,18,"Pack of 12 action figures (female)" 18  9   5.50    2013-01-02 00:00:00.0000000 True    6   2013-01-02 07:00:00.0000000
4   2   76  8   "\"The '

After (female)", the \t doesn't get replaced at all but it should as it replaces \t in first and second row. What am I doing wrong, any ideas?

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

0

You can use

const paragraph = '1\t1\t150\t18\t\"Pack of 12 action figures (variety)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n2\t1\t151\t21\t\"Pack of 12 action figures (male)\"\t21\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n3\t1\t152\t18\t\"Pack of 12 action figures (female)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n4\t2\t76\t8\t\"\\\"The ';
const regex = /("[^"]*")|\t/g;
const found = paragraph.replace(regex, (whole_match,group_one) => group_one || ',' );

console.log(found);

The /("[^"]*")|\t/g regex matches all occurrences of strings between double quotation marks capturing them into Group 1 or TAB char in other contexts.

If Group 1 matches, the replacement is Group 1 value, else, the replacement is a comma.

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