Javascript 如何使用angularjs过滤器突出显示多个搜索关键字?
一旦用户得到结果,我想突出显示结果中的搜索关键字,当用户搜索一个关键字时,它会突出显示,但当搜索多个关键字时,它仍然只突出显示一个搜索关键字 如何使用下面的过滤器突出显示所有搜索关键字 filter.jsJavascript 如何使用angularjs过滤器突出显示多个搜索关键字?,javascript,angularjs,angular-filters,Javascript,Angularjs,Angular Filters,一旦用户得到结果,我想突出显示结果中的搜索关键字,当用户搜索一个关键字时,它会突出显示,但当搜索多个关键字时,它仍然只突出显示一个搜索关键字 如何使用下面的过滤器突出显示所有搜索关键字 filter.js angular.module('loggingApp').filter('trusted', function($sce) { return function(text, phrase) { console.log("phrase",phrase);
angular.module('loggingApp').filter('trusted', function($sce) {
return function(text, phrase) {
console.log("phrase",phrase);
if (phrase) {
var data = text.replace(/[|&;$%@"<>()+,]/g, "");
data = data.replace(new RegExp('(' + phrase + ')', 'gi'),
'<span class="highlighted">$1</span>');
return $sce.trustAsHtml(data);
}
text = text.replace(/[|&;$%@"<>()+,]/g, "");
return $sce.trustAsHtml(text);
};
});
数据文本和短语
In below example i passed multiple strings ["3c72a612-4879-4877-b415-bf89ab110630","125981"] but it only highlight last one 125981.also from ctrl it sends one phrase `125981` as you see in phrase output from filter.
Text
[2017-06-16T02:48:36-04:00]zld00826infon/a[routes.event] ########## Message added to processing queue ########## 3c72a612-4879-4877-b415-bf89ab110630 { workerId: 6 pid: 125981 }
Phrase
125981
在我看来,它应该是有效的。(在Chrome中测试了RegExp)。您能给出一个输入示例吗?(文本和短语)。@Arg0n我添加了数据示例,请查看。
$scope.$on('displaySearchResults',function(e,data){
$scope.searchStr = data.searchStr;
$scope.showMessages = data.messageObj;
});
In below example i passed multiple strings ["3c72a612-4879-4877-b415-bf89ab110630","125981"] but it only highlight last one 125981.also from ctrl it sends one phrase `125981` as you see in phrase output from filter.
Text
[2017-06-16T02:48:36-04:00]zld00826infon/a[routes.event] ########## Message added to processing queue ########## 3c72a612-4879-4877-b415-bf89ab110630 { workerId: 6 pid: 125981 }
Phrase
125981