How to parse a browser cookie
Jan 13, 2021
// read all
document.cookie

// read one
const data = (document.cookie.match(/test=([^;]+)/) || [])[1]

// write
const test = "test";
// value has 24 hours to live on the client
const date = new Date();
date.setHours(date.getHours() + 24);
document.cookie = `refresh_token=${test};samesite=strict;expires=${date.toUTCString()}`;

To make a session cookie (lives with the window), don't specify expires. More cookie attributes defined here.

tags:
regex, parse, cookie, js, match
js match semicolon or end of line
Get updates