Javascript Cookie 通过正则表达式名称获取

1 javascript regex cookies jquery

您好,假设我在浏览器缓存中有此 cookie,如何通过正则表达式名称获取 cookie

name:  value:
text1  something
test   something
text2  something
4      bla
Run Code Online (Sandbox Code Playgroud)

就像获取 cookies 正则表达式 ('test'+onlynumbers)

回报

text1  something
text2  something
Run Code Online (Sandbox Code Playgroud)

mae*_*ics 5

像这样的东西应该有效(未经测试):

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..."]
Run Code Online (Sandbox Code Playgroud)