2

I'm trying to create manual browser cookie emulation using Node.js.

Basically I have two strings, once containing the current cookie and the other containing new or updated values to be added to the cookie. I want to update the old string with the new one by replacing or adding values. How can I achieve this via javascript?

For example:

PREF=ID=2191324fe41c6152:FF=0:TM=1320223272:LM=1320223272:S=H6-vdbEpgEzHAknb;
Foo=kfjaofjiof382f3gio;
expires=Fri, 01-Nov-2013 08:41:12 GMT; path=/; HTTPonly;

Is updated with

PREF=ID=435:FF=2:TM=1320223272:LM=1320223272:S=zzHAknb; Name=23;
expires=Fri, 01-Nov-2013 08:41:12 GMT; path=/; HTTPonly;

Returns

PREF=ID=435:FF=2:TM=1320223272:LM=1320223272:S=zzHAknb; Name=23;
Foo=kfjaofjiof382f3gio;
expires=Fri, 01-Nov-2013 08:41:12 GMT; path=/; HTTPonly;
2
  • Split strings, put data of first in object, overwrite keys with second object, stringify again? Commented Nov 2, 2011 at 8:54
  • May I recommend checking out this question? Commented Nov 2, 2011 at 16:40

1 Answer 1

0
function string_replace(haystack, find, sub) //replaces all occurences of find with sub
 {
    return haystack.split(find).join(sub);
 } 

function string_add(haystack,upgrade){return haystack+upgrade;}

I think you can do the rest.

Thanks to bobnice of Javascript - how to replace a sub-string?.

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

1 Comment

or if you want to replace the values, go with a regex approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.