Cookie通过regex名称获取

Cookie通过regex名称获取

本文介绍了Javascript Cookie通过regex名称获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  name:value:$ 

b $ b text1 something
test something
text2 something
4 bla

like get cookies regex('test'+ onlynumbers)



返回

  text1 something 
text2 something


解决方案

这样的东西应该工作(未测试):

  var getCookieByMatch = function(regex){
var cs = document。 cookie.split(/; \s * /),ret = [],i;
for(i = 0; i if(cs [i] .match(regex)){
ret.push(cs [i]);
}
}
return ret;
};
getCookieByMatch(/ ^ text\d + = /); // => [text1 = x; ...,text2 = y ...]


Hi how can i get cookies by regex names assuming that i have this cookies in browser cache

name:  value:
text1  something
test   something
text2  something
4      bla

like get cookies regex ('test'+onlynumbers)

returns

text1  something
text2  something
解决方案

Something like this should work (untested):

var getCookieByMatch = function(regex) {
  var cs=document.cookie.split(/;\s*/), ret=[], i;
  for (i=0; i<cs.length; i++) {
    if (cs[i].match(regex)) {
      ret.push(cs[i]);
    }
  }
  return ret;
};
getCookieByMatch(/^text\d+=/); // => ["text1=x;...", "text2=y..."]

这篇关于Javascript Cookie通过regex名称获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:17