You need to filter empty strings out of the list of words (in case of several consecutive spaces) to avoid weird stuff in the highlighting.