[Home] >  String >  Reverse the order of lines of a text

Reverse the order of lines of a text

JavaScript version

const reverseLines = (str) => str.split(/\r?\n/).reverse().join('\n');

TypeScript version

const reverseLines = (str: string): string => str.split(/\r?\n/).reverse().join('\n');

Examples

reverseLines(`one
two
three
`
);

/* Output */
/*
three
two
one
*/