要编辑的正则表达式,以便使用所选的搜索多个单词

regex to edit in order to search multiple words with chosen

我正在使用 chosen (chosen website) 使 selectlist 更加用户友好,搜索功能只搜索一个确切的词。

我的意思是如果列表包含

和用户类型

搜索功能为

escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");

我如何编辑它以允许用户搜索 1 个以上的词?

这是完整的 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();
  }
};

您可以使用 String.indexOf:

过滤数组
var data = [
    "Hello",
    "Hello dude",
    "Hello dudes",
    "Hi dudes"
];

function stringContains(str, value) {
    return str.indexOf(value) > -1;
}

function arrayFilter(arr, value) {

    var result = [];

    for (var i = 0; i < arr.length; i+=1) {

        var subject = arr[i];

        if (stringContains(subject, value)) {
            result.push(subject);
        }
    }

    return result;
}

console.log(arrayFilter(data, "Hello")); // ["Hello", "Hello dude", "Hello dudes"]
console.log(arrayFilter(data, "Hello dude")); // ["Hello", "Hello dude"]

或者,如果您不关心 prehistoric browsers or using polyfills,您可以简单地使用:

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 + '}$');

这里有一个测试来展示它是如何工作的(不确定全局脚本是否相同):

var searchText = 'abc edf';
var splitSearchText = searchText.replace(/[-[\]{}()*+?.,\^$|#]/g, "\$&").split(' ');
var escapedSearchText = ('^((' + splitSearchText.join('|') + ')\s?){' + splitSearchText.length + '}$');
var zregex = new RegExp(escapedSearchText, 'i');

alert(zregex.test('abc'));
alert(zregex.test('abc bcd'));
alert(zregex.test('abc edf'));
alert(zregex.test('edf abc'));

$(".chzn-select").chosen({"search_contains": true});是即使用户键入的内容不在文本开头也能进行搜索的解决方案