javascriptjquery-ui-autocompletetypeahead.js

Search within string using typeahead.js?


I would like typeahead.js to behave like jqueryui autocomplete with regards to how it matches items. Using jqueryui autocomplete it's possible to search inside the text items. In typeahead it's only from the beginning of the string.

Autocomplete example: https://jsfiddle.net/sebmade/swfjT/

Typeahead example: http://twitter.github.io/typeahead.js/examples/

With autocomplete, it seems like it's possible to assign a comparison function, but I haven't found anything like that in typeahead.

If I have a list that contains the item "Equestrian (Horses)" then I would like to get a match if I start writing "o".


Solution

  • Typeahead.js code as is will look for prefix matches, as you correctly say. There is a "trick" though: every datum may also contain a tokens element, which as the Typeahead documentation says is "a collection of strings that aid typeahead.js in matching datums with a given query".

    The prefix matching is done against tokens. If you don't supply a tokens value for one of your datums, its value is tokenized (space-separated) for you. However, you could supply tokens to get what you want. For example, in your case you would supply a value of tokens that is all the unique substrings of all the words in your query string.

    I suggest "all unique substrings of length >= 2", btw.