View Full Version : string.replace() problemo

10-24-2004, 05:37 AM
The sting handeling function replace(RegExp, replacetext) dosn't seem to be working properly. Okay, here's my problem, I have a textarea on my page and I save it's data to a cookie. When I load the cookie, it's fine, as long as it's on one line. If the user presses enter and saves the cookie, somehow the new line get's replaced with "__". This is not good if your trying to save about 100 lines of code. it all becomes 1 line with "__" wherever "\n" was. in the open cookie function I made, I tried to replace "__" with "\n" but nothing happens. :confused:

function savehtml()
document.cookie = "Html=" + htmlcode.value + ";";
function openhtml()
htmlstart = document.cookie.indexOf("=") + 1;
htmlend = document.cookie.indexOf(";");
var temp = new String();
temp = document.cookie.substring(htmlstart, htmlend);
temp.replace("__", "\n");
htmlcode.value = temp;

any Ideas?

10-24-2004, 07:08 AM
var regEx = /__/gi;
temp.replace(regEx, "\n");

10-24-2004, 01:39 PM
Replace is nondestructive - it returns the new value, but the variable does still contain the original.

10-24-2004, 03:26 PM
Thanks, I'll see what I can do. :rolleyes:

10 minutes later: Yay, it works! :thumbsup: