Reputation: 363
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 "Bethany" but not "Elizabeth". I need to keep this functionality. Except for parentheses, suggestions should only match the beginning of each word.
Thanks for the help.
Upvotes: 2
Views: 797
Reputation: 149
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.
Upvotes: 3
Reputation: 56
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).
Upvotes: 4