Skip to content

Commit

Permalink
update regex for highlighting (#2692)
Browse files Browse the repository at this point in the history
fixes #2623
  • Loading branch information
koenpunt authored Sep 19, 2016
1 parent 358ade5 commit 35979c1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class AbstractChosen

searchText = this.get_search_text()
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
zregex = new RegExp(escapedSearchText, 'i')
regex = this.get_search_regex(escapedSearchText)
highlightRegex = this.get_highlight_regex(escapedSearchText)

for option in @results_data

Expand All @@ -184,7 +184,7 @@ class AbstractChosen

if option.search_match
if searchText.length
startpos = option.search_text.search zregex
startpos = option.search_text.search highlightRegex
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length)
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos)

Expand All @@ -207,6 +207,11 @@ class AbstractChosen
regex_flag = if @case_sensitive_search then "" else "i"
new RegExp(regex_anchor + escaped_search_string, regex_flag)

get_highlight_regex: (escaped_search_string) ->
regex_anchor = if @search_contains then "" else "\\b"
regex_flag = if @case_sensitive_search then "" else "i"
new RegExp(regex_anchor + escaped_search_string, regex_flag)

search_string_match: (search_string, regex) ->
if regex.test search_string
return true
Expand Down

0 comments on commit 35979c1

Please sign in to comment.