簡體   English   中英

根據js中的用戶輸入搜索單詞

[英]Searching words based on user input in js

所以我有以下設置,用戶輸入文本,我想看看是否有任何輸入的字符匹配單詞。 當我輸入時,這一切都很有效。

const input = 'memb';

但如果做這樣的事情。

 const input = 'member has';

然后它返回false。 如果它找到匹配的字符,它應該保持為真,成員匹配。 成員也是匹配,因為字符m,e,m,b,e,r仍然是匹配事件,盡管h,a,s與任何其他單詞不匹配。

任何人都知道如果字符匹配我怎么能讓它繼續返回true?

 const input = 'member has'; const inputLower = input.toLowerCase(); const words = ['member', 'support', 'life']; const result = words.some(word => { const words = word.split(','); return words.some(r => r.toLowerCase().includes(inputLower)); }); console.log('result = ', result); 

您可以添加反向邏輯,還可以檢查inputLower是否包含r

 const input = 'memb has'; let inputLower = input.toLowerCase(); const words = ['member', 'support', 'life']; const result = words.some(word => { const words = word.split(','); return words.some(r => { if (~inputLower.indexOf( " " )) { // only check the first word if there are multiple inputLower = inputLower.substring( 0, inputLower.indexOf( " " ) ); } return r.toLowerCase().includes(inputLower) || inputLower.includes(r.toLowerCase()); }); }); console.log('result = ', result); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM