I need text that fits nicely in an autocomplete dropdown. This required more strategy than it would initially appear. Using hidden text or fadeout option didn't work well because the matched text string was often outside of the visible frame of its context. The ellipsis approach currently is a bit heavier weight than I need. "Show more" works well, but not in the context of the dropdown box; I don't want the user to worry about anything except selection of the option. I just want a line or two of context centred around the matched token. There are some related answers, but nothing that quite addresses the need for both n from the right and n from the left.

```
var haystack = "r4 r3 r2 r1 needle r1 r2 r3";
var needle = "needle";
var delim = " ";
var numWords = 5;
var trimmedText = trimToNeighboringText(haystack, needle, numWords, delim);

console.log("With " + numWords + " neighboring words: \"" + trimmedText + "\"");

```