谁能解析下js getcookie 的正则?

mengyaoren mengyaoren 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
看了一个getcookie函数,里面的正则不是很明白,谁能解释下吗?谢谢
代码是这样的:
function getCookie(name)
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}

...全文

相关推荐

javascript 读写cookie

javascript 读写cookie对象 Cookie.write() //写 Cookie.read() //读 Cookie.remove() //删除

易寒

javascript 中Cookie读、写与删除操作

主要介绍了javascript 中Cookie读、写与删除操作的相关资料,需要的朋友可以参考下

weixin_38719578

javascript操作cookie

javascript操作cookie /** * Read the JavaScript cookies tutorial at: * [url]http://www.netspade.com/articles/javascript/cookies.xml[/url] */ /** * Sets a Cookie with the given name and value. * * name Name of the cookie * value Value of the cookie * [expires] Expiration date of the cookie (default: end of current session) * [path] Path where the cookie is valid (default: path of calling document) * [domain] Domain where the cookie is valid * (default: domain of calling document) * [secure] Boolean value indicating if the cookie transmission requires a * secure transmission */ function setCookie(name, value, expires, path, domain, secure) { [removed]= name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); } /** * Gets the value of the specified cookie. * * name Name of the desired cookie. * * Returns a string containing value of specified cookie, * or null if cookie does not exist. */ function getCookie(name) { var dc = [removed]; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else { begin += 2; } var end = [removed].indexOf(";", begin); if (end == -1) { end = dc.length; } return unescape(dc.substring(begin + prefix.length, end)); } /** * Deletes the specified cookie. * * name name of the cookie * [path] path of the cookie (must be same as path used to create cookie) * [domain] domain of the cookie (must be same as domain used to create cookie) */ function deleteCookie(name, path, domain) { if (getCookie(name)) { [removed] = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; } } -------------------------------------------------------------------------------- function setCookies(name,value) { var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days*24*60*60*1000); [removed] = name + "="+ escape(value) +";expires="+ exp.toGMTString(); } function getCookies(name) { var arr = [removed].match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null; } function delCookies(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) [removed]=name +"="+cval+";expires="+exp.toGMTString(); } -------------------------------------------------------------------------------- function getCookie(name) { var dc = [removed]; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else { begin += 2; } var end = [removed].indexOf(";", begin); if (end == -1) { end = dc.length; } return unescape(dc.substring(begin + prefix.length, end)); } 这个应该不行吧. ======================================= //另一个读取COOKIE的方法 function GetCookieData(sL) { var sRet=""; var sC=""+[removed]; if(sC.length>0) { var aC=sC.split(";",100); var iC=aC.length; for(var i=0;i<iC;i++) { if(aC[i].indexOf(sL+"=")!=-1) { var aRet=aC[i].split("="); sRet=unescape(aRet[1]); break; } } } return sRet; }

zhangqi88250787

Javascript 读后台cookie代码

由于asp的cookie是经过url编码的,所以读之前要unescape一下。

weixin_38696877

JS设置cookie、读取cookie、删除cookie

JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的。 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一种情况,在某个用例流程中,由A页面跳至B页面,若在A页面中采用JS用变量temp保存了某一变量的值,在B页面的时候,同样需要使用JS来引用temp的变量值,对于JS中的全局变量或者静态变量的生命周期是有限的,当发生页面跳转或者页面关闭的时候,这些变量的值会重新载入,即没有达到保存的效果。解决这个问题的最好的方案是采用cookie来保存该变量的值,那么如何来设置和读取cookie呢? 首先需要稍微了

weixin_38722464
评论 3
收藏 已收藏
分享
返回顶部
mengyaoren
JavaScript 87,983 成员 224,681 内容