Javascript Cookie使用正则表达式删除或删除(正则表达式)

时间:2018-09-12 05:23:57

标签: javascript regex cookies session-cookies

Cookies to remove or delete with regex

The Cookie is Not Returned in the normal document.cookie function

这是我要删除或删除以JavaScript正则表达式(正则表达式)开头为“搜索”的最后5个cookie的网站

正常的document.cookie函数中未返回Cookie

我尝试了很多技巧和代码,但无法正常工作...请帮助

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

JSONDecoder().decode(type:request.objectClass, data:dataFromService)

用法:

    function deleteCookie(name) {
        document.cookie = name + "=; expires=" + (new Date(0)).toUTCString() + ";";
    };
    function findCookies(name) {
        var r=[];
        document.cookie.replace(new RegExp("("+name + "[^= ]*) *(?=\=)", "g"), function(a, b, ix){if(/[ ;]/.test(document.cookie.substr(ix-1, 1))) r.push(a.trim());})
        return r;
    };

或使用它,如果,您仅需要5个(从最后开始):

findCookies("search").forEach(function(fullName){deleteCookie(fullName);});

答案 1 :(得分:1)

//to get all the cookies 
var cookiesArray = document.cookie.split(";"); <br>
//loop through the array and check if the cookie name is what we want
for(var i = 0; i < cookiesArray.length; i++)
{
    //remove if any extra space
    var cookie = cookiesArray[i].trim();
    //to get the cookie name
    var cookieName = cookie.split("=")[0];

    // If the prefix of the cookie's name matches the one specified(i.e search), remove it
    if(cookieName.indexOf("search") === 0) {

        // Remove the cookie
        document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
    }
}
相关问题