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  Get count of each character in a string using JavaScript

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.