Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2004
    Location
    On my computer
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question string.replace() problemo

    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.

    Code:
    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?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
        var regEx = /__/gi; 
        temp.replace(regEx, "\n");
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Replace is nondestructive - it returns the new value, but the variable does still contain the original.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New to the CF scene
    Join Date
    Oct 2004
    Location
    On my computer
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I'll see what I can do.

    10 minutes later: Yay, it works!
    Last edited by Gmakermaniac!!!; 10-24-2004 at 03:35 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •