TIL: Javascript newline replacement

Trying to get rid of any newline characters in a field of an HTML text input form is not straightforward. These can be input by a user pressing Shift+Enter, or pasting in text that already includes newlines. I thought this would do it:

var sp = ” “;
var stripped = userInput.replace(/[\ca-\cz]/gm, sp);

That didn’t work so I substituted  .replace(/\R/gm, sp), but no.

Today I learned that only this would do the job:
var sp = ” “;
var userLinks = userLinks.replace(new RegExp( “\\n”, “g” ),sp);

That will replace CR, CR LF, and LF.

Roy

Sorry, the comment form is closed at this time.

A personal blog about a few of my interests … other than Visual Thinking