Javascript Cookie удалить или удалить с помощью регулярных выражений (регулярное выражение) - PullRequest
0 голосов
/ 12 сентября 2018

Cookies to remove or delete with regex

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

Это сайт, который я хочу удалить или удалить последние 5 файлов cookie, начиная с "поиск""с помощью регулярных выражений javascript, как мне это сделать

Cookie не возвращается в обычной функции document.cookie

Я перепробовал много трюков и кодов и не работал ..Пожалуйста, помогите

Ответы [ 2 ]

0 голосов
/ 12 сентября 2018
//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;";
    }
}
0 голосов
/ 12 сентября 2018

Вы можете попробовать это:

    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;
    };

Использование:

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

или используйте это, , если , вам нужно только 5 из них (из последних):

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