搜索字符串角度打字稿的一部分

时间:2021-03-12 22:43:31

标签: angular typescript angular7

我正在尝试专门在姓名和姓氏中搜索字符串,例如,我的字符串“oscar daniel gomez lopez”,我需要搜索“oscar gomez”或“oscar lopez”或“oscar”

我有这个功能,但搜索是直系的

if(that.removeAccents(String(elem[that.dash_keys[i]])).toLowerCase().indexOf(val) !== -1){
     find = true;
     break;
}

这个函数的结果是

  • 字符串“oscar daniel gomez lopez”搜索“oscar gomez”我得到错误文本
  • 字符串“oscar daniel gomez lopez”搜索“oscar daniel”我得到真实文本
  • 字符串“oscar daniel gomez lopez”搜索“oscar lopez”我得到了错误

谢谢

3 个答案:

答案 0 :(得分:0)

我不认为我真的理解你想要什么。你能更精确吗? 关于你的功能,我认为 String.prototype.includes() 做你想做的

答案 1 :(得分:0)

不太明白你想要什么,但如果你想返回 true 而不考虑位置,每个名字/姓氏,试试这个:

if(val.toLowerCase().split(' ').every(name=>that.removeAccents(String(elem[that.dash_keys[i]])).toLowerCase().includes(name)))
{
    find = true;
    break;
}

答案 2 :(得分:0)

我认为你可以这样做:

  findInSentence(originalSentence: string, wordsToSearchFor: string): boolean {
    const wordsToArray = wordsToSearchFor.split(' ');
    return wordsToArray.every(word => originalSentence.toLowerCase().includes(word.toLowerCase()));
  }