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!