Skip to main content

Remove empty lines of a text document

JavaScript version

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

TypeScript version

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

Examples

removeEmptyLines(`red

green
blue

yellow`);

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

Comments