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
  • 3,997
  • 3
  • 19
  • 32

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
  • 21,281
  • 1
  • 21
  • 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
  • 39,326
  • 11
  • 49
  • 54