简体   繁体   English

Lodash-搜索“字符串”是否包含以“。”开头的单词

[英]Lodash - Search if String contains a word that start with

I have an array of elements (let's say a list of fitness trainings). 我有一系列的元素(比如说健身训练的清单)。

I have a search bar, and I would like to create a search feature to find a specific training . 我有一个搜索栏,我想创建一个搜索功能以查找特定的培训

I know how to filter the array to: 我知道如何将数组过滤为:

  1. Keep trainings where name start with string typed in the search bar 继续进行训练,训练中名称应以在搜索栏中键入的字符串开头

     array = array.filter(function(item) { return item.name.toLowerCase().startsWith(searchFilter.toLowerCase()); }); 

Exemple: Search = "El"... 范例:搜寻=“ El” ...

Results -> ["Elizabeth", "Elisa"] 结果-> ["Elizabeth", "Elisa"]

Results not fetched -> ["Open Elios"] 无法获取结果-> ["Open Elios"]

  1. Keep trainings where name contains string typed in the search bar 进行训练,使名称包含在搜索栏中键入的字符串

     array = _.filter(array, function(item) { return _.includes(item.name.toLowerCase(), searchFilter.toLowerCase()); }); 

Exemple: Search = "El"... 范例:搜寻=“ El” ...

Results -> ["Elizabeth", "Open Elios", "Camel"] 结果-> ["Elizabeth", "Open Elios", "Camel"]

But it's not what I would like: 但这不是我想要的:

  1. Keep training where name contains words starting with string typed in the search bar 继续训练名称包含在搜索栏中键入字符串开头的单词的地方

Exemple: Search = "El"... 范例:搜寻=“ El” ...

Results -> ["Elizabeth", "Open Elios"] 结果-> ["Elizabeth", "Open Elios"]

Results not fetched -> ["Camel"] 无法获取结果-> ["Camel"]

You can use regex for that, something like: 您可以为此使用正则表达式,例如:

 const result = ["Elizabeth", "Open Elios", "Camel"].filter(v => /\\bel/i.test(v)); console.log(result); 

The important part is \\b which means word boundary, basically this expression /bel is testing if the value contains something that starts with a word boundary and is fallowed by el . 重要的部分是\\b ,它表示单词边界,基本上,该表达式/bel正在测试该值是否包含以单词边界开头且被el赋予的值。

Here is a complete example that constructs the regex from a string: 这是一个从字符串构造正则表达式的完整示例:

 const array = ["Elizabeth", "Open Elios", "Camel"]; const searchFilter = 'El'; const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'); const result = array.filter(value => new RegExp(`\\\\b${escapeRegExp(searchFilter)}`, "i").test(value)); console.log(result); 

You could create a dynamic regex with a word boundary \\b using RegExp constructor 您可以使用RegExp构造函数创建带有单词边界\\b的动态正则RegExp

 function filter(arr, word) { const regex = new RegExp(`\\\\b${word}`, 'i'); return arr.filter(n => regex.test(n)) } console.log(filter(["Elizabeth", "Elisa", "Open Elios", "Camel"], "El")) 

Apart form regex, you could also apply filter twice with the last one returning a boolean value for the filter of the current string to be included. 除了正则表达式外,您还可以应用两次filter ,最后一个返回一个布尔值,表示要包含的当前字符串的filter。

 var search_string = 'eli'; search_string = search_string.toLowerCase(); const arr = ["Elizabeth", "Open Elios", "Camel"]; const result = arr.filter(value => value.split(' ').filter(token => token.toLowerCase().startsWith(search_string)).length > 0); console.log(result); 

Update: 更新:

As suggested by @Titus in the comments, you can also use some() method to filter data which would immediately return true if it finds a token which starts with your search string, instead of collecting all tokens in a filter that would start with your search string. 正如@Titus在评论中所建议的那样,您还可以使用some()方法来过滤数据,如果该数据找到以搜索字符串开头的令牌,则将立即返回true,而不是将所有令牌收集在以您的字符串开头的过滤器中搜索字符串。

 var search_string = 'eli'; search_string = search_string.toLowerCase(); const arr = ["Elizabeth", "Open Elios", "Camel"]; const result = arr.filter(value => value.split(' ').some(token => token.toLowerCase().startsWith(search_string))); console.log(result); 

You can use regex to build a dynamic pattern based on value 您可以使用正则表达式基于值构建动态模式

(?:^|[ ])${value}
  • (?:^|[ ]) - Match start of string or space (?:^|[ ]) -匹配字符串或空格的开始
  • ${value} - dynamic value coming from search bar ${value} -来自搜索栏的动态值

 let search = (value) => { const reg = `(?:^|[ ])${value}` const regex = new RegExp(reg,'gi') const result = ["Elizabeth", "Open Elios", "Camel"].filter(v => regex.test(v)); return result } console.log(search('el')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM