SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get a value out of document.cookie with regular expression

    I'm trying to use regular expressions instead of string functions to return a value belonging to a given name in document.cookie.
    First, traditionally I did this and it, of course, works fine:

    function readCookie(name)
    {

    var c=document.cookie;
    var pairs=c.split(";");
    for(var i=0; i< pairs.length; i++)
    {
    var p=pairs[i].split("=");
    while(p[0].charAt(0)==' '){p=p.substring(1,p.length);}
    if(p[0]==name){return p[1];}
    }

    return false;
    }

    So far so good..,but I want to use back referencing to do the same,so I tried this:
    function readCookie(name)
    {


    var str= name +"="+"([^;]+);";
    /*intended meaning:concatanate the string value in the variable called name with = and a regular expression pattern that is targeting the value part after the given name.
    */
    /*Then create a new RegExp object with this string, because I don't know
    how I could use the name variable in a regular literal.. Can I? I know the RegExp object takes a string with the pattern as its first argument*/
    var reg= new RegExp(str,"gi");
    /*and use the part in bracket to back reference to the interesting value part*/
    if(reg.test(c))
    {
    return "$1";
    }
    else{ return false;}
    }
    .. but it's all wrong. Can you tell me where the logic above fails?

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get the results from treating a match as an array. Also, I don't think that "$1" has any special meaning in Javascript.

    So, this seems to work:

    Code:
    <script type="text/javascript" charset="utf-8">
      var cookie = "foo=bar;alpha=beta"
      var pattern = "=([^;]+)";
      alert(cookie.match("foo"+pattern)[1]);
      alert(cookie.match("alpha"+pattern)[1]);
    </script>
    I've also removed the last semicolon from the pattern, I'm not sure if a cookie is guaranteed to end with one.

    hth,
    Douglas
    Hello World

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by petroczi View Post
    if(reg.test(c))
    {
    return "$1";
    }
    else{ return false;}[/INDENT]
    }
    I believe $n works only in the String().replace second parameter string.
    I tried this once:
    Code:
    function rxReadCookie(cName)
    {
     var rslt=new RegExp(cName+"=([^;]+)\\;?", "i").exec(document.cookie?unescape(document.cookie):"");
      
     return rslt ? rslt[1] : "";
    }
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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