Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

363
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!