EN
TypeScript - remove all new line characters from string
0
points
In this short article, we are going to show how to remove all new line characters from string in TypeScript.
Quick solution.
const LINE_EXPRESSION: RegExp = /\r\n|\n\r|\n|\r/g; // expression symbols order is very important
const text: string = 'line 1\n' + 'line 2\r' + 'line 3\r\n' + 'line 4\n\r' + 'line 5';
const convertedText: string = text.replace(LINE_EXPRESSION, '');
console.log(convertedText);
Output:
line 1line 2line 3line 4line 5
Note:
''inreplace()method can be replaced with any character that will be inserted in new line characters place.