Angularjs-utils高亮过滤器在搜索时破坏应用程序

Angularjs ui utils highlight filter breaks application on search

本文关键字:应用程序 搜索 高亮 过滤器 Angularjs-utils      更新时间:2023-09-26

我使用的是Angular ui utils高亮过滤器,我有以下代码:

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

当我使用像[或()这样的特殊字符进行搜索时,我会得到角度异常和应用程序中断。

SyntaxError:无效的正则表达式:/(/:未终止的组在新RegExp(本机)在v(http://localhost:50463/EIA/source/dist/vendor.min.js:72:1157)在i(http://localhost:50463/EIA/source/dist/vendor.min.js:38:92754)在cr.|(http://localhost:50463/EIA/source/dist/vendor.min.js:38:86832)在h.constant(http://localhost:50463/EIA/source/dist/vendor.min.js:38:92126)在Object.e(http://localhost:50463/EIA/source/dist/vendor.min.js:38:101832)v.摘要(http://localhost:50463/EIA/source/dist/vendor.min.js:38:57280)v.适用(http://localhost:50463/EIA/source/dist/vendor.min.js:38:58986)在http://localhost:50463/EIA/source/dist/client.js:1007:31在http://localhost:50463/EIA/source/dist/vendor.min.js:38:64888未定义

我试着使用了ng消毒库,但还是出现了同样的错误。

求你了,我该怎么解决?

您应该转义RegExp输入,因为(是正则表达式的特殊字符:

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

然后,只需使用它:

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