js中的模糊搜索( 正則表達式)


此案例在vue中實現

搜索設備ID示例

<input 
       type="text" 
       name="" 
       placeholder="搜索設備ID" 
       v-model="searchData" 
       @input="search()" />

search() {
      if (this.searchData != '') {
        this.isSearch = true //當用戶有輸入時,顯示搜索結果組件
      } else {
        this.isSearch = false
      }
      let data = []
      if (this.deviceList.length != 0 && this.searchData) { 
        //當設備列表不為空且用戶有輸入時
        let str = `\S*${this.searchData}\S*`
        let reg = new RegExp(str)
        this.deviceList.map((item) => {
          if (reg.test(item.nDeviceID)) {
            data.push(item)
          }
        })
        //使用正則和map方法,遍歷數組對象,將匹配項push到新數組中
      }
      //渲染新數組
      this.searchRes = data
 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM