Doug Fir Doug Fir - 7 months ago 28
Javascript Question

regex for this cookie value?

I have a cookie with a long value, I have pasted an example below. I need to split on name value pairs and return, in this case "id" which from below corresponds to


I'm having trouble reading what syntax the developers have used here (I'm a analyst not dev engineer). In the past I have seen name value pairs used with = or '|' syntax as a separator.

It looks like the name value pair is like so:


var usd = document.cookie.match(/(^| )user_session_data\=([^;]*)(;|$)/)[0]
var arr = usd.split('%22')
var ind = arr.indexOf('id');
var pair = arr[ind];

But pair is equal to "id" only, I was hoping to get this array value
which I could then split.

What would be a clean regex to get 167814329?

Here is the example cookie value to search through:



Hanlet Escaño definitely has the most accurate answer, but if you really wanted to use regex you can. It was unclear whether you wanted id%22%3a[ID]%2C or just the ID, so here's both:

Full string: (?:^| )user_session_data\=[^;]*(id%22%3A\d+%2C)[^;]*(?:;|$)

Just the ID: (?:^| )user_session_data\=[^;]*id%22%3A(\d+)%2C[^;]*(?:;|$)

In the future, if you're having trouble with a regex, this site can be extremely useful.