Is it possible to replace all carriage returns in a string via .replace?

Is it possible to replace all carriage returns in a string with the .replace function? I've found quite a few complex functions to do it, but was wondering if it could be simplified with just a regex through .replace?

Thanks!


Both \n (new line) and \r (carraige return) create a new line. To replace all instances of both at the same time:

s.replace(/[\n\r]/g, '');

Note that you might want to replace them with a single space rather than nothing.


Here how to do it

str = str.replace(/\r/gm,'newChar');

By default, Javascript replace() replaces the first occurance. The way around it is to set the first parameters as a regex.