I would like to replace the domain
of a cookie string:
var cookieText = 'test=value; path=/; domain=.mydomain.com';
cookieText.replace(/[Dd]omain=(\.?)([a-zA-Z0-9\.]*);/, function(match, dot, domain){
return dot + myfunction(domain)
});
But it seems mycallback function is never called. What is wrong with the code?
That's because you regex doesn't match any substring in your cookieText
string. This happens because you pattern expects a ;
at the end of each substring, but that doesn't happen to domain=.mydomain.com
. Try this regex instead:
/[Dd]omain=(\.?)([a-zA-Z0-9\.]*);?/
Then it will work:
var cookieText = 'test=value; path=/; domain=.mydomain.com';
cookieText.replace(/[Dd]omain=(\.?)([a-zA-Z0-9\.]*);/, function(match, dot, domain){
console.log(match);
return dot + myfunction(domain);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.