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=

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

to parse the cookies we need to do lot of work it seems , and why regex is not a preferred way ?
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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.