Angularjs ui utils 突出显示过滤器在搜索时中断应用程序

Angularjs ui utils highlight filter breaks application on search

我正在使用 Angular ui-utils 高亮过滤器,我有以下代码:

<span data-ng-bind-html="organization.level1Name | highlight:vm.search"></span>

当我使用 [ 或 ( 等特殊字符进行搜索时,出现 angular 异常并且应用程序中断。

SyntaxError: Invalid regular expression: /(/: Unterminated group at new RegExp (native) at v. (http://localhost:50463/EIA/source/dist/vendor.min.js:72:1157) at i (http://localhost:50463/EIA/source/dist/vendor.min.js:38:92754) at cr.| (http://localhost:50463/EIA/source/dist/vendor.min.js:38:86832) at h.constant (http://localhost:50463/EIA/source/dist/vendor.min.js:38:92126) at Object.e (http://localhost:50463/EIA/source/dist/vendor.min.js:38:101832) at v.$digest (http://localhost:50463/EIA/source/dist/vendor.min.js:38:57280) at v.$apply (http://localhost:50463/EIA/source/dist/vendor.min.js:38:58986) at http://localhost:50463/EIA/source/dist/client.js:1007:31 at http://localhost:50463/EIA/source/dist/vendor.min.js:38:64888 undefined

我尝试使用 ng-sanitize 库,但我仍然遇到同样的错误。

拜托,我该如何解决?

你应该escape your RegExp输入因为(是正则表达式的特殊字符:

function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\^$\|]/g, "\$&");
}

那么,就用它吧:

new RegExp(escapeRegExp(search), 'gi')