简体   繁体   中英

get typeahead.js to ignore parenthesis when finding suggestions

I am using typeahead.js for suggestions. It's working well except one issue. My suggestions have parentheses, like this:

"Elizabeth (Liz)"
"Robert (Bob)"
"Michael (Mike)"

typeahead.js only matches the beginning of words, so if a user types "Liz" it will not suggest the first item. If you type "(Liz" it will match. But users should be able to start typing a name or nickname to see the matched suggestion.

How can I get typeahead.js to ignore the parentheses and look for matches in both words?

working example on jsfiddle: https://jsfiddle.net/laurakurup/mru39nk2/

Important note: I do not want typeahead.js to look for matches anywhere in a string. I know that would fix it, but when there are tens of thousands of recommendations that won't be helpful. For example, typing "Beth" suggests " Beth any" but not "Eliza beth ". I need to keep this functionality. Except for parentheses, suggestions should only match the beginning of each word.

Thanks for the help.

if you change the datumTokenizer to:

datumTokenizer: Bloodhound.tokenizers.nonword

The predictions will be performed at the beginning of the word in the same way but excluding the symbols and not only splitting for white spaces.

Then you get suggestion for Eli and Liz for Elizabeth (Liz) .

you can use the Bloodhound.tokenizers.nonword tokenizer as a datumTokenizer

internally it uses this expression split(/\\W+/) which will split the stings by any character that is not part of the word. For example symbols and spaces.

here's a link to the js fiddle js fiddle

optionally you may want to implement your own tokenizer to split by spaces and parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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