Skip to content

How to remove all new line breaks from a string in JavaScript?

The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.

function removeLineBreaks(str) {
    return str.replace( /[\r\n]+/gm, "" );

removeLineBreaks("Hello,\n Good Afternoon. \nHope you are all having good time. \n Thanks!");
//Hello, Good Afternoon. Hope you are all having good time.  Thanks!
See also  Funny String - Hackerrank Challenge - C# Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.