Reading cookies the regular expression way

While taking a look on the 2nd 24ways article for 2009, I was really surprised to read that “The Web Storage API is basically cookies on steroids, a unhealthy dosage of steroids. Cookies are always a pain to work with. First of all you have the problem…


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou

While taking a look on the 2nd 24ways article for 2009, I was really surprised to read that “The Web Storage API is basically cookies on steroids, a unhealthy dosage of steroids. Cookies are always a pain to work with. First of all you have the problem of setting, changing and deleting them. Typically solved by Googling and blindly relying on PPK’s solution. (bold is mine)

Of course, there’s nothing wrong with PPK’s solution. It works just fine. However, I always thought his readCookie() function was too verbose and complicated for no reason. It’s a very common example of someone desperately trying to avoid using a regular expression. I googled for “javascript read cookie” and to my surprise, all examples found in the first results were very similar. I never understood why even experienced developers are so scared of regular expressions. Anyway, if anyone wants a shorter function to read a cookie, here’s what I use:

function readCookie(name) { // Escape regexp special characters (thanks kangax!) name = name.replace(/([.*+?^=!:${}()|[\]\/\\])/g, ‘\\$1’);

var regex = new RegExp(‘(?:^|;)\\s?’ + name + ‘=(.*?)(?:;|$)’,‘i’), match = document.cookie.match(regex);

return match && unescape(match[1]); // thanks James! }

Update: Function updated, see comments below.

I’ve been using it for years and it hasn’t let me down. :)

Probably lots of other people have come up and posted something similar before me (I was actually very surprised that something like this isn’t mainstream), but I’m posting it just in case. :)


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou


Print Share Comment Cite Upload Translate Updates
APA

Lea Verou | Sciencx (2009-12-03T00:00:00+00:00) Reading cookies the regular expression way. Retrieved from https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/

MLA
" » Reading cookies the regular expression way." Lea Verou | Sciencx - Thursday December 3, 2009, https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/
HARVARD
Lea Verou | Sciencx Thursday December 3, 2009 » Reading cookies the regular expression way., viewed ,<https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/>
VANCOUVER
Lea Verou | Sciencx - » Reading cookies the regular expression way. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/
CHICAGO
" » Reading cookies the regular expression way." Lea Verou | Sciencx - Accessed . https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/
IEEE
" » Reading cookies the regular expression way." Lea Verou | Sciencx [Online]. Available: https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/. [Accessed: ]
rf:citation
» Reading cookies the regular expression way | Lea Verou | Sciencx | https://www.scien.cx/2009/12/03/reading-cookies-the-regular-expression-way/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.