0

i have a cookie string like this 'user=sravan;XSRF-TOKEN=1212143;session=random' i need to check for the XSRD-TOKEN in the cookie string, if we have the XSRF-TOKEN in the string then need to replace the value with 'test'

expected new string is 'user=sravan;XSRF-TOKEN=test;session=random'

i tried this (?<=XSRF-TOKEN).*$ but it is selecting the entire string after XSRF-TOKEN=

sravan ganji
  • 4,774
  • 3
  • 25
  • 37

2 Answers2

2

You could use (?<=XSRF-TOKEN=)([^;]+), example:

const str = 'user=sravan;XSRF-TOKEN=1212143;session=random';
const processed = str.replace(/(?<=XSRF-TOKEN=)([^;]+)/, "test");
console.log(processed);

But a better solution will be to parse the cookies and recreate the string.

Titus
  • 22,031
  • 1
  • 23
  • 33
0

This should only only select up until ;

(?<=XSRF-TOKEN)[^;]+

Or if you only like to select whats after = to ;

(?<=XSRF-TOKEN=)[^;]+

'user=sravan;XSRF-TOKEN=1212143;session=random'

Jotne
  • 40,548
  • 12
  • 51
  • 55