Languages
[Edit]
EN

TypeScript - remove all new line characters from string

0 points
Created by:
Welsh
902

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: '' in replace() method can be replaced with any character that will be inserted in new line characters place.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join