[JavaScript] Regular Expression and Snippet to Replace Line Break Characters \r\n \r \n with Spaces

Tadashi Shigeoka ·  Tue, December 20, 2016

I’ll introduce a regular expression and snippet to replace line break characters with spaces in JavaScript.

JavaScript
var text = 'spam\\r\\nspam\\rspam\\nspam';
console.log(text);
// spam
// spam
// spam

var replacedText = text.replace(/\\r\\n|\\r|\\n/g, ' ');
console.log(replacedText);
// spam spam spam spam

It replaces any pattern of \r\n, \r, or \n with a space.

Reference Information

That’s all from the Gemba.