基于正则的前端数据筛选功能实现

TL;DR

gist

核心

keywords(val) {
        if (val) {
          // eg. '(' => '\(','+' => '\+'
          val = `${val}`.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
          let origin = this.search;
          let reg = new RegExp(`${val}`);
          if (this.keywords && origin && Array.isArray(origin)) {
            this.searchItem = origin.filter(item => reg.test(item.name));
          } else {
            this.searchItem = this.search;
          }
        } else {
          this.searchItem = this.search;
        }
Table of Contents