Javascript 要编辑的正则表达式,以便使用选定的

Javascript 要编辑的正则表达式,以便使用选定的,javascript,jquery,regex,jquery-chosen,Javascript,Jquery,Regex,Jquery Chosen,我使用selected()使selectlist更加用户友好,搜索功能只搜索一个确切的单词 我的意思是,如果列表包含 测试->安装社区 测试->安装通讯->管理大楼 测试->安装通讯->停车 测试->安装社区->小建筑 和用户类型 “安装”,一切正常(用户参见4条记录) “安装通讯”,用户看到0结果(因为当有超过1个单词时,搜索不起作用) 搜索功能是 escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\

我使用selected()使selectlist更加用户友好,搜索功能只搜索一个确切的单词

我的意思是,如果列表包含

  • 测试->安装社区
  • 测试->安装通讯->管理大楼
  • 测试->安装通讯->停车
  • 测试->安装社区->小建筑
和用户类型

  • “安装”,一切正常(用户参见4条记录)
  • “安装通讯”,用户看到0结果(因为当有超过1个单词时,搜索不起作用)
搜索功能是

escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
我如何编辑此内容以允许用户搜索多个单词

以下是完整的JS函数:

AbstractChosen.prototype.winnow_results = function() {
  var escapedSearchText, option, regex, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref;
  this.no_results_clear();
  results = 0;
  searchText = this.get_search_text();
  escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  zregex = new RegExp(escapedSearchText, 'i');
  regex = this.get_search_regex(escapedSearchText);
  _ref = this.results_data;
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    option = _ref[_i];
    option.search_match = false;
    results_group = null;
    if (this.include_option_in_results(option)) {
      if (option.group) {
        option.group_match = false;
        option.active_options = 0;
      }
      if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
        results_group = this.results_data[option.group_array_index];
        if (results_group.active_options === 0 && results_group.search_match) {
          results += 1;
        }
        results_group.active_options += 1;
      }
      if (!(option.group && !this.group_search)) {
        option.search_text = option.group ? option.label : option.text;
        option.search_match = this.search_string_match(option.search_text, regex);
        if (option.search_match && !option.group) {
          results += 1;
        }
        if (option.search_match) {
          if (searchText.length) {
            startpos = option.search_text.search(zregex);
            text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
            option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
          }
          if (results_group != null) {
            results_group.group_match = true;
          }
        } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
          option.search_match = true;
        }
      }
    }
  }
  this.result_clear_highlight();
  if (results < 1 && searchText.length) {
    this.update_results_content("");
    return this.no_results(searchText);
  } else {
    this.update_results_content(this.results_option_build());
    return this.winnow_results_set_highlight();
  }
};
AbstractSelected.prototype.winnow\u结果=函数(){
var escapedSearchText,option,regex,results,results_组,searchText,startpos,text,zregex,_i,_len,_ref;
这个。无结果清除();
结果=0;
searchText=this.get_search_text();
escapedSearchText=searchText.replace(/[-[\]{}()*+?,\\^$\\\s]/g,“\\$&”);
zregex=newregexp(escapedSearchText,'i');
regex=this.get\u search\u regex(转义搜索文本);
_ref=此。结果\u数据;
对于(_i=0,_len=_ref.length;_i<_len;_i++){
选项=_ref[_i];
option.search\u match=false;
结果_组=null;
如果(该选项包括结果中的选项(选项)){
如果(选项组){
option.group_match=false;
option.active_options=0;
}
if((option.group\u array\u index!=null)&&this.results\u data[option.group\u array\u index]){
结果组=此.results\u数据[选项组数组索引];
if(results\u group.active\u options==0&&results\u group.search\u match){
结果+=1;
}
结果_组。主动_选项+=1;
}
if(!(option.group&&!this.group_search)){
option.search_text=option.group?option.label:option.text;
option.search\u match=this.search\u string\u match(option.search\u text,regex);
if(option.search\u match&&!option.group){
结果+=1;
}
如果(选项搜索匹配){
if(searchText.length){
startpos=option.search_text.search(zregex);
text=option.search\u text.substr(0,startpos+searchText.length)+''+option.search\u text.substr(startpos+searchText.length);
option.search_text=text.substr(0,startpos)+''+text.substr(startpos);
}
if(结果组!=null){
结果组匹配=真;
}
}else if((option.group\u array\u index!=null)&&this.results\u data[option.group\u array\u index].search\u match){
option.search_match=true;
}
}
}
}
此.result_clear_highlight();
if(结果<1&&searchText.length){
此。更新\u结果\u内容(“”);
返回此。无搜索结果(searchText);
}否则{
this.update_results_内容(this.results_选项_build());
返回此值。winnow_results_set_highlight();
}
};

您可以使用以下方法筛选阵列:


从纯regexp方法(我不知道如何选择)中,您可以尝试以下方法:

var splitSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&").split(' ');
escapedSearchText = ('^((' + splitSearchText.join('|') + ')\\s?){' + splitSearchText.length + '}$');
下面是一个测试,演示它是如何工作的(不确定全局脚本是否工作相同):
var searchText='abc edf';
var splitSearchText=searchText.replace(/[-[\]{}()*+?,\\^$\\\\\\\]]/g,“\\$&”).split(“”);
var escapedSearchText=('^('+splitSearchText.join('|')+')\\s?{'+splitSearchText.length+'}$');
var zregex=new RegExp(escapedSearchText,'i');
警报(zregex.test('abc'));
警报(zregex.test(“abc bcd”);
警报(zregex.test(“abc edf”);

警报(zregex.test(“edf abc”)$(“.chzn select”).selected({“search\u contains”:true});即使用户键入的内容不在文本开头,也可以进行搜索,这不是我的经验。只要单词是连续的,就应该匹配多个单词(即,“Hello dude”将匹配“Hello dude”和“Hello dudes”。您能用您正在使用的选择框发布一个JSFIDLE吗?这是我的JSFIDLE。您可以看到它和您期望的完全匹配。不应该有任何理由对正则表达式做任何花哨的事情。@tobylaroni:嗯,这很奇怪……这是我的JSFIDLE:@tobylarony:通过放置我的选项,它对您的小提琴不起作用too@tobylaroni不知道为什么这不是RNING吗?提前感谢这确实是一种Rube Goldberg方法,并没有解决使用选定项的问题。我不想投反对票,但这确实不是对这个问题的适当回答,我希望其他遇到选定项问题的人不会将此作为他们的解决方案。@tobylaroni为什么现在很难编写一段简单的代码?它可以工作,而且比使用正则表达式更快。@谢谢,但它在应用程序中不起作用:-/好的,对不起,也许你应该看看选项(group_match,search_match,…)。显然,组件必须搜索短语,所以regex不是解决方案(我没有发布好问题)
function arrayFilter(arr, value) {
    return arr.filter(function (subject) {
        return subject.indexOf(value) > -1;
    }
}
var splitSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&").split(' ');
escapedSearchText = ('^((' + splitSearchText.join('|') + ')\\s?){' + splitSearchText.length + '}$');