Get cookie by name

前端 未结 30 1754
野的像风
野的像风 2020-11-22 03:58

I have a getter to get the value from a cookie.

Now I have 2 cookies by the name shares= and by the name obligations= .

I want to

30条回答
  •  孤城傲影
    2020-11-22 04:40

    Here is a pretty short version

     function getCookie(n) {
        let a = `; ${document.cookie}`.match(`;\\s*${n}=([^;]+)`);
        return a ? a[1] : '';
    }
    

    Note that I made use of ES6's template strings to compose the regex expression.

提交回复
热议问题