HTML5 Localstorage&jQuery:删除以特定单词开头的localstorage键

我有2个应用程序与localstorage一起工作,我想知道如何删除所有以note-和todo-开头的键。 我知道localstorage.clear()会清除所有内容,但这不是我的目标。

以下是我在本地存储中的示例: 在此处输入图像描述

我想删除所有todo- *点击按钮和所有note- *与其他按钮点击使用jquery。

非常感谢

Object.keys(localStorage) .forEach(function(key){ if (/^todo-|^note-/.test(key)) { localStorage.removeItem(key); } }); 

我使用了类似于@Ghostoy的方法,但我想输入一个参数,因为我在代码中的几个地方调用了它。 我无法在正则表达式中使用我的参数名称,所以我只使用了substring。

 function ClearSomeLocalStorage(startsWith) { var myLength = startsWith.length; Object.keys(localStorage) .forEach(function(key){ if (key.substring(0,myLength) == startsWith) { localStorage.removeItem(key); } }); }