trim break lines of string in javascript

One way to trim break lines from a string in Javascript is by using a regular expression with the replace method. The regular expression /[\r\n]+/g matches one or more occurrences of the \r or \n characters.

Here's an example:

index.tsx
let str = "Hello,\nWorld!\n\n";
let result = str.replace(/[\r\n]+/g, '');

console.log(result); // Output: "Hello,World!"
122 chars
5 lines

In the code above, we define a string str with two break lines (\n). We use the replace method to replace these characters with an empty string ''. The resulting string is stored in the result variable.

Now, the result variable contains the trimmed string without break lines. We use the console.log method to output the result to the console.

gistlibby LogSnag