Replacing special characters like the newline character '\n' (Unix) or both '\n' and '\r' (Windows) is rather straightforward using Javascript's handy builtin regular expression-based replace method for strings. The following Javascript code snippet will replace all newlines in the text in my_string string with a single space, which can be useful for all sorts of text manipulation operations.


var my_string = "text that may include\nnewline characters";
var my_string_no_newlines = my_string.replace(/\n/g, " ");

And that's all there is to it. The regular expression /\n/ matches \n's and the trailing g means to do it globally for everywhere the expression is found (otherwise it would only perform the replacement on the first occurence).