如何将无序字符串与Javascript正则表达式匹配

如何将无序字符串与Javascript正则表达式匹配,javascript,regex,Javascript,Regex,我用javascript编写了一个实时过滤器,它从字段中获取一个值,并隐藏表中不匹配的行 我使用的正则表达式非常简单:/inputValue/I 虽然这非常有效,但它只匹配顺序正确的字符。例如: inputValue = test string to match = this is a test sentence 这个例子会匹配,但如果我尝试: inputValue = this sentence string to match = this is a test sentence 这将不匹配

我用javascript编写了一个实时过滤器,它从字段中获取一个值,并隐藏表中不匹配的行

我使用的正则表达式非常简单:/inputValue/I

虽然这非常有效,但它只匹配顺序正确的字符。例如:

inputValue = test
string to match = this is a test sentence
这个例子会匹配,但如果我尝试:

inputValue = this sentence
string to match = this is a test sentence
这将不匹配,因为输入值不符合顺序

我如何编写一个有序但可以跳过单词的正则表达式

以下是我当前使用的循环:

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

  var comparisonString = liveFilterDataArray[i],
    comparisonString = comparisonString.replace(/['";:,.\/?\\-]/g, '');

  RE = eval("/" + liveFilterValue + "/i");

  if (comparisonString.match(RE)) {
    rowsToShow.push(currentRow);
  }
  if(currentRow < liveFilterGridRows.length - 1) {
    currentRow++;
  } else {
    currentRow = 0;
  }
}
for(变量i=0;i
非常感谢您抽出时间


Chris

您可以在空格上拆分输入字符串,然后按顺序为每个单词运行筛选器。

建议使用而不是eval

它将创建
this.*句|句.*this/i


删除
+'|'+words.reverse().join(“.”)
如果您只想查找
这个…..句子而不是
句子…..这个

可能
RE=eval(“/”+liveFilterValue.split(“”)。join(“|+”/i”)
这是一个技巧,有一个警告,它会找到包含任何单词的结果,即inputValue=这句话第一个要匹配的字符串=这是一个测试句子第二个要匹配的字符串=这是一个测试段落,它将在两个字符串上匹配,因为单词“this”“在两个中都发生,因为它只应匹配第一个字符串,因为该字符串包含两个单词。谢谢你的帮助。
var words = liveFilterValue.split(" ");
var searchArg = (words.length==1)?words:words.join(".*")+'|'+words.reverse().join(".*")
var RE = new RegExp(searchArg,"i");