Check cookie value

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Check cookie value

Post by agibsonsw »

Hello.
I have the following JS code to check if a cookie name exists using a regular expression:

Code: Select all

var regX = new RegExp("\\b" + cookieName + "=([^;]*)");
var found = regX.exec(document.cookie);
There must be a similar expression that I could use to check if a cookie value exists?

Does someone have something similar? Or perhaps if you could describe the format of a cookie I could work out what sequence of characters I should be looking for to check for a value.

(I've found lots of examples that split the cookie and have used one to check the value, but this seems quite messy when a regular expression should be able to do the same thing.)

Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Check cookie value

Post by agibsonsw »

Hello.

I was making some progress with

Code: Select all

var regX = new RegExp("(=" + cookieValue + ";)");
but I might have to revert to the full search based on splitting the cookie.

The problem is that the cookie name/values are escaped/encoded and the symbols used have a different meaning to RegExp. Never mind, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Check cookie value

Post by agibsonsw »

I do like to answer my own questions occasionally! To check for an escaped or encoded cookie name or value requires the escaped name/value to also be escaped for use within a regular expression. The follow helper function and two functions will 'quickly' check for the existence of either a cookie name or value (case insensitive).

Code: Select all

// Escapes characters for use with a regular expression
String.prototype.EscapeR = function () { return this.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); }

function Name_Exists ( cookie_name )
    {
        if (document.cookie.length == 0) return false;
        var testName = escape(cookie_name);
        testName = testName.EscapeR();
        var myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','gi');
        return myReg.test(document.cookie);
    }
    function Value_Exists ( cookie_value )
    {
        if (document.cookie.length == 0) return false;
        var testName = encodeURI(cookie_value);     // or escape(cookie_value) if appropriate
        testName = testName.EscapeR();
        var myReg = new RegExp('(=)' + testName + '(;|$)','gi');
        return myReg.test(document.cookie);
    }
I wanted to use regular expressions rather than having to split the cookie and loop through it's array each time.

(I haven't tested them for hours on end but they appear to be doing a good job.)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.