Search code examples
jqueryjquery-cookie

How to delete any cookie where the cookie name start with "test"


I generate cookie with a name that start with "test" followed by a number using jQuery-Cookie Plugin

How can I remove all cookies that start with "test"

normally "if I know the exact cookie name I would do this

$.removeCookie('test10575'); 

but in this call all I know that the cookie starts with the "test"

How to delete any cookie where the cookie name start with "test"?


Solution

  • Try this

    $.each($.cookie(), function (name, value) {
      if (/^test/.test(name)) {
        $.removeCookie(name);
      }
    });
    

    Example