2012-11-02 121 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

相关问题