我正在尝试专门在姓名和姓氏中搜索字符串,例如,我的字符串“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;
}
这个函数的结果是
谢谢
答案 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()));
}