Skip to main content
InterviewHubPRO
Section Overview
Q361

Question 361 of 536

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

67%
JavaScript & V8 Internals (536 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

How to remove all line breaks from a string

senior LevelCode ExampleAsync & PromisesEvent LoopHTTP & REST APIs

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

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