diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js
index 8daeee9c75..79bf263ca4 100644
--- a/src/typeahead/test/typeahead.spec.js
+++ b/src/typeahead/test/typeahead.spec.js
@@ -142,6 +142,34 @@ describe('typeahead tests', function () {
});
});
+ describe('typeaheadHighlight', function () {
+
+ var highlightFilter;
+ beforeEach(inject(function (typeaheadHighlightFilter) {
+ highlightFilter = typeaheadHighlightFilter;
+ }));
+
+ it('should higlight a match', function () {
+ expect(highlightFilter('before match after', 'match')).toEqual('before match after');
+ });
+
+ it('should higlight a match with mixed case', function () {
+ expect(highlightFilter('before MaTch after', 'match')).toEqual('before MaTch after');
+ });
+
+ it('should higlight all matches', function () {
+ expect(highlightFilter('before MaTch after match', 'match')).toEqual('before MaTch after match');
+ });
+
+ it('should do nothing if no match', function () {
+ expect(highlightFilter('before match after', 'nomatch')).toEqual('before match after');
+ });
+
+ it('issue 316 - should work correctly for regexp reserved words', function () {
+ expect(highlightFilter('before (match after', '(match')).toEqual('before (match after');
+ });
+ });
+
describe('typeahead', function () {
var $scope, $compile, $document;
diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js
index 93e4ca7914..079dfd3cd4 100644
--- a/src/typeahead/typeahead.js
+++ b/src/typeahead/typeahead.js
@@ -207,7 +207,12 @@ angular.module('ui.bootstrap.typeahead', [])
})
.filter('typeaheadHighlight', function() {
+
+ function escapeRegexp(queryToEscape) {
+ return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
+ }
+
return function(matchItem, query) {
- return (query) ? matchItem.replace(new RegExp(query, 'gi'), '$&') : query;
+ return query ? matchItem.replace(new RegExp(escapeRegexp(query), 'gi'), '$&') : query;
};
});
\ No newline at end of file