In Js, the value of the specified key in the cookie name is fetched with the rule.

1. The string in the cookie fetched by the foreground is a connection of multiple cookie, for example, these two types:

var str = "OnlineCookie=OnlineCookie&UserType=11111111111111111&UID=39&UserName=; UID=; UserType=; UserName=";
 
var str2 = "OnlineCookie=OnlineCookie&UserType=11111111111111111&UID=39&UserName=%e5%bc%a0%e4%bf%8a; UID=; UserType=; UserName=; UserCookie=ZZSAPP&UnitName="

2. What I need to get is the value of cookie after the specified name, for example:
getCookie (str, "OnlineCookie", "UserType"); / / return 111111111111111
getCookie (str, "OnlineCookie", "UserName"); / / return "Zhang San"
getCookie (str2, "ZSAPP", "UnitName"); / / return "Beijing"

3. I used to write it in an array. But it feels so complicated, so I want to do it in a regular way

.
function getCookie(str,userCookieName,name) {
    var cookie = str;   
    if (cookie) {
        var temp = cookie.split(";");
        for (var k = 0, max = temp.length; k < max; kPP) {
            if (temp[k].indexOf(userCookieName) > -1) {
                var arr = temp[k].split("&");
                for (var i = 0; i < arr.length; iPP) {
                    var arr1 = arr[i].split("=");
                    for (var j = 0; j < arr1.length; jPP) {
                        if (arr1[0] == name) {
                            return arr1[1];
                            break;
                        }
                    }
                }
            }
           

        }
        
    }    
    return "";
}

3. How can this function be written in Js"s regular expression?

function getCookie(str,userCookieName,name){
}

use URLSearchParams.


function getCookieValue(key, cookieStr) {
    var result = new RegExp(`(?:^|\s)${key}=([^;]*)`).exec(cookieStr)
    return result && result[1]
}

function getCookie(str,userCookieName,name){
    var regex=new RegExp('\\b'+userCookieName+'\\b.*?&'+name+'=([^&;]*)','g');
    var result=regex.exec(str);
    return result&&result[1];
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e991dd-46474.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e991dd-46474.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?