Home > Enterprise >  Remove new lines within and around a specific text blok
Remove new lines within and around a specific text blok

Time:10-14

I have a text files that contains similar text (I placed \n as an indicator of new line, so it does not exists withing a text):

\n
---
\n
\n
not fixed
number of 
lines
\n
\n
---

Other text
More text
...

The text between triple lines is not fixed, could be many lines of text there. I want to remove all the new lines within and on the top of the triple lines:

---
not fixed
number of 
lines
---

Other text
More text
...

Currently I found how to delete the first line, but I am struggling to think how to omit the rest:

myText = myText.split('\n').slice(1).join('\n'));

CodePudding user response:

Use myText.replace(/^\n/gm, "")

Example/Demo:

let text = `
---


not fixed
number of 
lines


---

Other text
More text`;

console.log(text.replace(/^\n/gm, ""));

CodePudding user response:

Something like this ought to do you:

const s1 = "...";
const s2 = s1.replace( /\n ---\n /g , '\n---\n' ) ;
  • Related