views:

1012

answers:

2

Hey guys,

I've been going at this problem for a solid couple hours now and having zero luck. No clue how this is even possible; I'll try to summarize.

I'm using TinyMCE to insert new content to a DB, that content is being sent back as an AJAX response after it is inserted into the DB and then shown on the page, replacing some old content. All of that isn't really relevant (as far as I can tell) to the problem, but it serves as a background to the problem.

Anyhow, the response text has '\n' appropriately wherever the content had line breaks. I can't seem to remove those damn '\n' for the life of me. I've tried a dozen regex/replace combos with zero luck. I've verified I am not losing my mind and that the code generally works by attempting to replace other words within that string and that works perfectly fine - it just will NOT replace '\n'. Here's some code I've used attempting to replace the '\n's:

responseText = responseText.replace(/\r|\n|\r\n/g, "");

responseText = responseText.replace(Array("\r", "\n", "\f", "\r\n", "\n"), "");

Niether of those do anything to the variable. I alert it immediately after to check for changes, nada. I have no idea if it will help, but here's a snippet of an example '\n' copy-pasted that will not disappear OR change.

High School transcript</li>\n<li>SAT/ACT

As a side note, I've tried doing this via PHP before the responseText is sent back to javascript with a similar replace & regex and it does NOT work either.

+5  A: 

Are you sure it's a newline and not a literal "\n" (that is an escaped newline) ?

Try this instead: (note the double backslash)

responseText = responseText.replace(/\\n/g, "");
Mark Renouf
Fantastic. I owe ya one. This worked and makes perfect sense after thinking about the situation. Man that was aggravating... Cheers!
Chris Cooper
+2  A: 
responseText = responseText.replace(/\n/g, "");

Don't forget to use the /g flag or else only the first one will be replaced!

Andreas Grech
Doesn't work, and is virtually the same as one I tried:responseText = responseText.replace(/\r|\n|\r\n/g, "");Yea?
Chris Cooper
well since you said "the response text has '\n' appropriately wherever the content had line breaks", I tried it with removing line breaks and it did remove line breaks.
Andreas Grech