"mehdi_mousavi" <me***********@gmail.com> writes:
function removeParam(str, name)
{
var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)');
Could be shortened to
var rgx = new RegExp(name + "=\\w*;?","gi");
rgx.global = true;
rgx.ignoreCase = true;
The "gi" above makes these two irrelevant too.
var matches = rgx.exec(str);
Here you should be aware of how the exec method works on regexps
with the global flag set.
It does not do all the possible matches in the string. Instead it
finds the first match and sets the property "index" on the regexp,
so the next call to exec starts at that index in the string (right
after the first match).
if(matches == null)
return str;
var i;
for(i = 0; i < matches.length; i++)
str = str.replace(matches[i], '');
return str;
}
Somewhat overkill:
return str.replace(rgx,"");
and the following call:
var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39';
alert(removeParam(cookie, 'vid'));
I actually would like to remove "vid=(whatever number)" from the above
string, so that it results in the following string:
ASPNETSESSION=WHATEVER
It will return
" ASPNETSESSION=WHATEVER; "
would someone please helps me to fix the problem? I've got no idea
what's wrong with the above pattern I've written.
I would suggest another approach, where you parse the cookie instead
of treating it as a flat string:
---
function splitCookie(cookie) {
var res = new Object();
var pairs = cookie.split(/;\s+/g);
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
res[pair[0]] = pair[1];
}
return res;
}
var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39';
var cookieData = splitCookie(cookie);
var aspsession = cookieData["ASPNETSESSION"];
alert(aspsession)
---
This is very simplified code. It does not make a special case for
keys that occour more than once, and it doesn not decode the cookie
keys or values, which might be encoded (using "escape" or url-encoding).
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'