JavaScript / ECMAScript

Cookie Header Value in JS

Parse name=value pairs from an HTTP `Cookie:` header value.

Try it in the JS tester →

Pattern

regexJS
([^=;\s]+)=([^;]+)   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("([^=;\\s]+)=([^;]+)", "g");
const input = "session=abc123; theme=dark; user_id=42";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

([^=;\s]+) captures the cookie name — characters that aren't equals, semicolon, or whitespace. = is the literal separator. ([^;]+) captures the value — everything up to the next semicolon, allowing spaces and special chars inside the value. Repeats globally to capture every cookie.

Examples

Input

session=abc123; theme=dark; user_id=42

Matches

  • session=abc123
  • theme=dark
  • user_id=42

Input

single=value

Matches

  • single=value

Input

no cookies

No match

Same pattern, other engines

← Back to Cookie Header Value overview (all engines)