[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Remove empty lines of a text document

Remove empty lines of a text document

JavaScript

const removeEmptyLines = (str) => str
.split(/\r?\n/)
.filter((line) => line.trim() !== '')
.join('\n')

Examples

removeEmptyLines(`red

green
blue

yellow
`
)

/* Output */
/*
red
green
blue
yellow
*/