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

325
Views
Regex to remove spaces between \n in block of text

In javascript, I am trying to write a regex that will remove all spaces between occurrences of \n.

For example, the string Test\n \n \n \n\n 1234 would turn into Test\n\n\n\n\n 1234.

How do I do this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use non-greedy matching of whitespace \s*? bracketed by lookbehind and lookahead assertions for \n, (expressed as (?<=\n) and (?=\n) respectively) and replace it with an empty string.

const input = "Test\n \n \n    \n\n 1234";
const result = input.replace(/(?<=\n)\s*?(?=\n)/g, '');

console.log(JSON.stringify(result));

about 4 years ago · Juan Pablo Isaza Report

0

let t = "test\n\n \n\n\n \n"

console.log(JSON.stringify(t.replaceAll(/(?<=\n)\s*?(?=\n)/g, '')))

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!