JS Coding Questions Logo
JS Coding Questions
#357💼 Interview💻 Code

How to remove all line breaks from a string

Advertisement

728x90

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.

javascript
1function remove_linebreaks( var message ) {
2  return message.replace( /[\r\n]+/gm, "" );
3  }

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

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 14

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
357of476