2012-11-02 120 views
1

我有這樣添加數字/數字和字母/之間的空格字符

(function($, window, document, undefined) { 
    $.fn.quicksearch = function (target, opt) { 

     var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({ 
      delay: 100, 
      selector: null, 
      stripeRows: null, 
      loader: null, 
      noResults: '', 
      bind: 'keyup', 
      onBefore: function() { 
       return; 
      }, 
      onAfter: function() { 
       return; 
      }, 
      show: function() { 
       this.style.display = ""; 
      }, 
      hide: function() { 
       this.style.display = "none"; 
      }, 
      prepareQuery: function (val) { 
       return val.toLowerCase().split(' '); 
      }, 
      testQuery: function (query, txt, _row) { 
       for (var i = 0; i < query.length; i += 1) { 
        if (txt.indexOf(query[i]) === -1) { 
         return false; 
        } 
       } 
       return true; 
      } 
     }, opt); 

     this.go = function() { 

      var i = 0, 
      noresults = true, 
      query = options.prepareQuery(val), 
      val_empty = (val.replace(' ', '').length === 0); 

      for (var i = 0, len = rowcache.length; i < len; i++) { 
       if (val_empty || options.testQuery(query, cache[i], rowcache[i])) { 
        options.show.apply(rowcache[i]); 
        noresults = false; 
       } else { 
        options.hide.apply(rowcache[i]); 
       } 
      } 

      if (noresults) { 
       this.results(false); 
      } else { 
       this.results(true); 
       this.stripe(); 
      } 

      this.loader(false); 
      options.onAfter(); 

      return this; 
     }; 

     this.stripe = function() { 

      if (typeof options.stripeRows === "object" && options.stripeRows !== null) 
      { 
       var joined = options.stripeRows.join(' '); 
       var stripeRows_length = options.stripeRows.length; 

       jq_results.not(':hidden').each(function (i) { 
        $(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]); 
       }); 
      } 

      return this; 
     }; 

     this.strip_html = function (input) { 
      var output = input.replace(new RegExp('<[^<]+\>', 'g'), ""); 
      output = $.trim(output.toLowerCase()); 
      return output; 
     }; 

     this.results = function (bool) { 
      if (typeof options.noResults === "string" && options.noResults !== "") { 
       if (bool) { 
        $(options.noResults).hide(); 
       } else { 
        $(options.noResults).show(); 
       } 
      } 
      return this; 
     }; 

     this.loader = function (bool) { 
      if (typeof options.loader === "string" && options.loader !== "") { 
       (bool) ? $(options.loader).show() : $(options.loader).hide(); 
      } 
      return this; 
     }; 

     this.cache = function() { 

      jq_results = $(target); 

      if (typeof options.noResults === "string" && options.noResults !== "") { 
       jq_results = jq_results.not(options.noResults); 
      } 

      var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults); 
      cache = t.map(function() { 
       return e.strip_html(this.innerHTML); 
      }); 

      rowcache = jq_results.map(function() { 
       return this; 
      }); 

      return this.go(); 
     }; 

     this.trigger = function() { 
      this.loader(true); 
      options.onBefore(); 

      window.clearTimeout(timeout); 
      timeout = window.setTimeout(function() { 
       e.go(); 
      }, options.delay); 

      return this; 
     }; 

     this.cache(); 
     this.results(true); 
     this.stripe(); 
     this.loader(false); 

     return this.each(function() { 
      $(this).bind(options.bind, function() { 
       val = $(this).val(); 
       e.trigger(); 
      }); 
     }); 

    }; 

}(jQuery, this, document)); 

我揣摩在哪?我怎麼能做出一個分流/數字和字母之間添加空間的代碼。導致一些人輸入例如「ip1500」,腳本不能匹配輸入和「ip 1500」之類的元素。我的問題在於我是初學者。

我試圖嘗試,但我不能得到它的工作。我也試過this

我發現這點,我認爲它可以在這裏完成,其中的一切都得到了一個「」(空格)分裂:

prepareQuery: function (val) { 
    return val.toLowerCase().split(' '); 
    }, 

將是非常好的,如果有人能幫助我。

回答

2

您鏈接的代碼無法正常工作,主要是因爲它使用的是不同的編程語言。從理論上講,它應該工作,但JavaScript不支持正則表達式lookbehinds(在目前這個時間)..

相反,我重新寫的代碼片段:

prepareQuery: function (val) { 
function isNotLetter(a){ 
return (/[0-9-_ ]/.test(a)); 
} 

var val=val.toLowerCase().split(""); 
var tempArray=val.join("").split(""); 
var currentIndex=1; 

for (var i=0;i<val.length-1;i++){ 
if (isNotLetter(val[i]) !== isNotLetter(val[i+1])){ 
tempArray.splice(i+currentIndex, 0, " "); 
currentIndex++; 
} 
} 
return tempArray.join(""); 
} 

既然你新的JavaScript,我要解釋它做了什麼。

  1. 它聲明prepareQuery一個函數來檢查字符串是否包含一個函[這可以在其他地方移動]
  2. 它再分裂val到一個數組,拷貝val內容到tempArray
  3. 索引被聲明(稍後解釋)
  4. 甲環製成,其中通過每一個字符進去val
  5. if語句檢測的電流是否字符(由循環設置的val[i])與其旁邊的字符相同(val[i+1])。
  6. IF任一個都與其它的不同(即當前字符是字母,而下一個不是),則空間被在該「指標」
  7. 索引遞增,並且用作加入到tempArray在#6中偏移
  8. 循環結束,將「數組」加入字符串並輸出結果。

DEMO: http://jsbin.com/ebitus/1/edit (的jsfiddle下跌了....)

編輯: 很抱歉,但我完全誤解了你的問題。你沒有提到你使用「quicksearch 「和jQuery。在這種情況下,我假設你有一個有名字的元素列表,並且你想用插件搜索它們...... 一個更簡單的方法來匹配用戶的查詢(如果沒有空間)是從查詢表中除去查詢本身的空間 - 儘管原始的反向方法將起作用(只是效率不高)[又名:擴展用戶查詢]

在這種情況下,同時從搜索表和用戶輸入剝離空間將是一個較好的方法

prepareQuery: function (val) { 
     return val.toLowerCase().replace(/ /ig,'').split(" "); 
    }, 
    testQuery: function (query, txt, _row) { 
     txt=txt.toLowerCase().replace(/ /ig,''); 
    for (var i = 0; i < query.length; i += 1) { 
     if (txt.indexOf(query[i]) === -1) { 
      return false; 
     } 
    } 
    return true; 

}

DEMO: http://jsfiddle.net/q9k9Y/3/

編輯2:

好像你的真正意圖是要在網站上創建一個全功能的搜索功能,而不是僅僅字母和數字之間添加空格。有了這個,我建議使用Quicksilver。我很想制定一個算法來擴展quickSearcher,但在目前我不能(時區)。相反,我建議使用水銀

http://jsbin.com/oruhet/12/

+0

非常感謝你,這工作得很好,但現在這拆分每一個字母和數字。例如:我輸入「pixma ip1500」並且腳本甚至匹配一個像這樣的元素:「pixam pim 015」會導致每個單獨的數字,並且來自輸入的字母位於li元素「pixam pim 015」中。換句話說,li元素包含來自輸​​入的每一個數字和字母。對不起,我的英語不好! – Kuba

+0

@xtramaster任何想法:)? – Kuba

+0

Finaly我修復了每個單一數字或字母的分割問題。我改變了你最後一行「return tempArray.join(」「);」到「return tempArray.join(」「)。split('');」。所以我只是添加這個「.split('');」 – Kuba

6

如果你想 「123abc345def」 到 「123 ABC 345 DEF」。替換功能可能有所幫助。代碼就是這樣。

var str = "123abc345def"; 
str = str.replace(/(\d+)/g, function (_, num){ 
    console.log(num); 
    return ' ' + num + ' '; 
}); 
str = str.trim(); 
+0

謝謝。我試圖將其包含在我的代碼中,我希望它能夠一起工作! – Kuba

相關問題