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

370
Visualizações
How to delete line in multiline string?

I have a multiline stock. How can I remove a specific line?

const str = `
  import 'C:\Users\Desktop\sanitize.js';
  val = ln + '\t';
  val += lc + '\t';
  val += f + '\t'; 
  val += id + '\t'; // id source
  val += sc + '\t\n';
`;

In this case, I need to delete lines where Import or reguired occurs, while maintaining line breaks.

My approach doesn't work, because there can be a line break inside the lines, and I'm trying to split the line by line breaks in order to achieve the result.

function sanitizeSandbox(code) {
  let disinfectedSandbox = '';

  if (typeof code === 'string' && code.length > 0) {
    const sandboxSplit = code.split('\n');
    
    for (const str of sandboxSplit) {
      if (!str.includes('import') && !str.includes('required')) {
        disinfectedSandbox += `${str}\n`;
      }
    }
  }

  return disinfectedSandbox;
}

Expected value:

`
 val = ln + '\t';
 val += lc + '\t';
 val += f + '\t'; 
 val += id + '\t'; // id source
 val += sc + '\t\n';
`

The value I get with my approach:

`
 val = ln + '\t';
 val += lc + '\t';
 val += f + '\t';
 val += id + '\t'; // id source
 val += sc + '\t
 \n';
`
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use this regex (derived from this answer):

(?:[^\n']|'[^']*')+

to match lines in your string (while ignoring newlines within ''), then filter based on whether or not the line contains import or required:

const str = `
  import 'C:\Users\Desktop\sanitize.js';
  val = ln + '\t';
  val += lc + '\t';
  val += f + '\t'; 
  val += id + '\t'; // id source
  val += sc + '\t\n';
`;

result = str.match(/(?:[^\n']|'[^']*')+/g)
  .filter(l => !l.match(/\b(import|require)\b/))
  .join('\n')
  
console.log(result)
console.log(JSON.stringify(result))

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