I'd like to know if someone can help me with regex on javascript.
So basically I have the following string as an example:
var str = "A\nB\n\n\nC\n\n\n\n\nD" and I want to convert it to "A\nB\n\nC\n\n\n\nD" using regex or any other way around to do this with javascript.
The idea here is remove only one empty line between lines with content. Is there any way to do this?
One possible approach:
var str = "A\nB\n\n\nC\n\n\n\n\nD"
var out = str.replace(/\n(\n+)/g, '$1')
console.log(out) // "A\nB\n\nC\n\n\n\nD"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With