pawelczak / EasyAutocomplete

JQuery autocomplete plugin
http://easyautocomplete.com
MIT License
729 stars 244 forks source link

Truncate search result #389

Closed nazimcoskun closed 4 years ago

nazimcoskun commented 5 years ago

Hi,

When searching in a long content, such as a blog post, is it possible to show a truncated search result, instead of the whole content? For instance:

Content: Lorem ipsum dolor sit amet consectetur adipiscing elit.

Search phrase: sit amet

Result to be shown: ... dolor sit amet consectetur ...

nazimcoskun commented 5 years ago

I found a solution using custom template and a regular expression to match the search string and words around it.

template: {
  type: "custom",
  method: function(value, item) {
    regex = new RegExp(/\w*\s*\w*<b>(.*?)<\/b>\w*\s*\w*/g);
    value = "... " + value.match(regex) + " ...";
    return value;
  }
}