Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

update regex for highlighting #2692

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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