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

365
Vistas
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 Respuestas
Responde la pregunta

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