FrontendDeveloper.in

JavaScript question detail

How to remove all line breaks from a string

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 remove_linebreaks( var message ) {
return message.replace( /[\r\n]+/gm, "" );
}

In the above expression, g and m are for global and multiline flags.

Back to all JavaScript questions
Get LinkedIn Premium at Rs 399