找到给定单词javascript的字符串

时间:2022-09-13 12:15:42

I have the following title: Banana & Walnut Cinnamon Bread

我有以下标题:香蕉和核桃肉桂面包

I've created a search function which allows the user to type in free text, when I enter Banana Bread the above item with the title: Banana & Walnut Cinnamon Bread will not show purely because I'm using indexOf, however if the title was : Banana Bread & Walnut Cinnamon then it would work because 'Banana Bread' is next to one another.

我创建了一个搜索功能,允许用户输入*文本,当我输入香蕉面包上面的项目标题:香蕉和核桃肉桂面包将不会完全显示因为我使用indexOf,但如果标题是:香蕉面包和核桃肉桂然后它会起作用,因为'香蕉面包'是彼此相邻的。

Can someone suggest a javscript function which will allow me to search the complete string and pick out the words that match what I've typed in?

有人可以建议一个javscript函数,它允许我搜索完整的字符串并挑选出与我输入的字符相匹配的单词吗?

This was the original:

这是原作:

  if (searchDataList[i].ttl.toLowerCase().indexOf(freeText) > -1) {
    addItem = true;
  } 
  else {
    addItem = false;
  }

Thanks

3 个解决方案

#1


0  

Try splitting your input string and then do the search for each individual word. In this way you can get results for different combination of words.

尝试拆分输入字符串,然后搜索每个单词。通过这种方式,您可以获得不同单词组合的结果。

#2


0  

You should first turn your searchDataList (I guess it's currently an array) into regexp, this way:

您应该首先将您的searchDataList(我猜它当前是一个数组)转换为regexp,这样:

  • assuming searchDataList is ['aa', 'bb', 'cc']
  • 假设searchDataList是['aa','bb','cc']

  • make the corresponding regexp /(aa|bb|cc)/g
  • 制作相应的正则表达式/(aa | bb | cc)/ g

Then you can submit the entered words to this regexp: the returned array lists the words found.

然后,您可以将输入的单词提交到此正则表达式:返回的数组列出找到的单词。

#3


0  

The simple way to do it is to split your search text into individual words and check each one...

这样做的简单方法是将搜索文本拆分为单个单词并检查每个单词...

var searchWords = freeText.split(' ');

searchWords = CleanWords(searchWords); // This removes words like '&', '-', 'the' 

for(var i = 0; i < searchDataList.length; i++){
  for (var n = 0; n < searchWords.length; n++){
    var word = searchWords[n].toLowerCase();
    var title = searchDataList[i].ttl.toLowerCase();

    if (title.indexOf(word) > -1){
      // addItem = true;
    } 
  }
}

If you don't want to do that then you could try pre build an index and check if the search words are in the index. Pseudo code below...

如果您不想这样做,那么您可以尝试预先构建索引并检查搜索词是否在索引中。下面的伪代码......

// Prebuild index
var index = {};
foreach(var item in searchDataList){
  var words = item.ttl.split(' ');
  words = CleanWords(words);   // This removes words like '&', '-', 'the' etc

  foreach(var word in words){
    word = word.toLowerCase();
    if (index[word]){
      index[word].Push(item.no);  // Build up array of search term indexes that contain this word.
    } else {
      index[word] = [item.no];
    }
  }
}

// Perform search
var searchWords = freeText.split(' ');
searchWords = CleanWords(searchWords);
foreach(var word in searchWords){
  var matches = index[word];
  foreach(var match in matches){
    // Do something with matches
    var matchedItem = searchDataList[match];
  }
}

Also have a look at the following answer for more discussion on speed of accessing items by key on an object in JavaScript... Array vs. Object efficiency in JavaScript

另请参阅以下答案,以了解有关JavaScript中对象上按键访问项目的速度的更多讨论... JavaScript中的数组与对象效率

You could also consider using var index = new Map(); rather than an object (I'm not sure if it is faster or not though).

您还可以考虑使用var index = new Map();而不是一个对象(我不确定它是否更快)。

#1


0  

Try splitting your input string and then do the search for each individual word. In this way you can get results for different combination of words.

尝试拆分输入字符串,然后搜索每个单词。通过这种方式,您可以获得不同单词组合的结果。

#2


0  

You should first turn your searchDataList (I guess it's currently an array) into regexp, this way:

您应该首先将您的searchDataList(我猜它当前是一个数组)转换为regexp,这样:

  • assuming searchDataList is ['aa', 'bb', 'cc']
  • 假设searchDataList是['aa','bb','cc']

  • make the corresponding regexp /(aa|bb|cc)/g
  • 制作相应的正则表达式/(aa | bb | cc)/ g

Then you can submit the entered words to this regexp: the returned array lists the words found.

然后,您可以将输入的单词提交到此正则表达式:返回的数组列出找到的单词。

#3


0  

The simple way to do it is to split your search text into individual words and check each one...

这样做的简单方法是将搜索文本拆分为单个单词并检查每个单词...

var searchWords = freeText.split(' ');

searchWords = CleanWords(searchWords); // This removes words like '&', '-', 'the' 

for(var i = 0; i < searchDataList.length; i++){
  for (var n = 0; n < searchWords.length; n++){
    var word = searchWords[n].toLowerCase();
    var title = searchDataList[i].ttl.toLowerCase();

    if (title.indexOf(word) > -1){
      // addItem = true;
    } 
  }
}

If you don't want to do that then you could try pre build an index and check if the search words are in the index. Pseudo code below...

如果您不想这样做,那么您可以尝试预先构建索引并检查搜索词是否在索引中。下面的伪代码......

// Prebuild index
var index = {};
foreach(var item in searchDataList){
  var words = item.ttl.split(' ');
  words = CleanWords(words);   // This removes words like '&', '-', 'the' etc

  foreach(var word in words){
    word = word.toLowerCase();
    if (index[word]){
      index[word].Push(item.no);  // Build up array of search term indexes that contain this word.
    } else {
      index[word] = [item.no];
    }
  }
}

// Perform search
var searchWords = freeText.split(' ');
searchWords = CleanWords(searchWords);
foreach(var word in searchWords){
  var matches = index[word];
  foreach(var match in matches){
    // Do something with matches
    var matchedItem = searchDataList[match];
  }
}

Also have a look at the following answer for more discussion on speed of accessing items by key on an object in JavaScript... Array vs. Object efficiency in JavaScript

另请参阅以下答案,以了解有关JavaScript中对象上按键访问项目的速度的更多讨论... JavaScript中的数组与对象效率

You could also consider using var index = new Map(); rather than an object (I'm not sure if it is faster or not though).

您还可以考虑使用var index = new Map();而不是一个对象(我不确定它是否更快)。