完全实用的方法:
document.cookie.split(';').filter(function(c) {
return c.trim().indexOf('word') === 0;
}).map(function(c) {
return c.trim();
});
Run Code Online (Sandbox Code Playgroud)
附带说明:
//Get a list of all cookies as a semicolon+space-separated string
document.cookie.split(';')
//Filter determines if an element should remain in the array. Here we check if a search string appears at the beginning of the string
.filter(function(c) {
return c.trim().indexOf('word') === 0;
})
//Map applies a modifier to all elements in an array, here we trim spaces on both sides of the string
.map(function(c) {
return c.trim();
});
Run Code Online (Sandbox Code Playgroud)
ES6:
document.cookie.split(';')
.filter(c => c.startsWith('word'));
Run Code Online (Sandbox Code Playgroud)