Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit ba668e3

Browse files
committed
Merge pull request #322 from stevvvn/master
Escaping of regexp characters in terms during highlighting
2 parents 73cf684 + 5caa0e1 commit ba668e3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/jquery.tokeninput.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,13 +697,18 @@ $.TokenList = function (input, url_or_data, settings) {
697697
}
698698
}
699699

700+
var regexp_special_chars = new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g');
701+
function regexp_escape(term) {
702+
return term.replace(regexp_special_chars, '\\$&');
703+
}
704+
700705
// Highlight the query part of the search term
701706
function highlight_term(value, term) {
702-
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
707+
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + regexp_escape(term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
703708
}
704709

705710
function find_value_and_highlight_term(template, value, term) {
706-
return template.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + value + ")(?![^<>]*>)(?![^&;]+;)", "g"), highlight_term(value, term));
711+
return template.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + regexp_escape(value) + ")(?![^<>]*>)(?![^&;]+;)", "g"), highlight_term(value, term));
707712
}
708713

709714
// Populate the results dropdown with some results

0 commit comments

Comments
 (0)