从数组中突出显示单词

4

我想给我在一个数组中的单词做标记。

$scope.arrayFilter=["mom","is","beautifull"];

但是只有当单词按照它们出现的顺序时,这个功能才能正常工作。我希望无论单词的顺序如何,只要匹配就应该被标记。如果我添加一个新单词到数组中,它也应该被标记。

https://jsfiddle.net/1x7zy4La/

<li ng-repeat="item in data ">
  <span ng-bind-html="item.title | highlight:arrayFilter"></span>
</li>

  $scope.arrayFilter=["mom","is","beautifull"];
  $scope.data = [{
    title: "mom is beautifull"
  }, {
    title: "my mom is great"
  }, {
    title: "I hate the matematics"
  }];
});

app.filter('highlight', function($sce) {
return function(text, arrayFilter) {
    var stringToDisplay = '';
    angular.forEach(arrayFilter,function(key,value){
        if(text.includes(key)){
          stringToDisplay =  stringToDisplay.concat(key).concat(" ");
        }
    })
   stringToDisplay =  stringToDisplay.substring(0, stringToDisplay.length - 1);
   return $sce.trustAsHtml(text.replace(new RegExp(stringToDisplay, 'gi'), '<span class="highlightedText">$&</span>'));

}
});

enter image description here

3个回答

6
问题出在你在拼接键(keys)- 把你的filter改为这个:
app.filter('highlight', function($sce) {
  return function(text, arrayFilter) {
    angular.forEach(arrayFilter, function(key, value) {
      if (text.includes(key)) {
        text = text.replace(new RegExp(key, 'gi'), '<span class="highlightedText">$&</span>')
      }
    })
    return $sce.trustAsHtml(text);
  }
});

请查看以下演示或更新的 jsfiddle

var app = angular.module('testApp', []);
app.controller('testCtrl', function($scope) {
  $scope.arrayFilter = ["is", "mom", "beautifull"];
  $scope.data = [{
    title: "mom is beautifull"
  }, {
    title: "my mom is great"
  }, {
    title: "I hate the matematics"
  }];
});



app.filter('highlight', function($sce) {
  return function(text, arrayFilter) {
    angular.forEach(arrayFilter, function(key, value) {
      if (text.includes(key)) {
        text = text.replace(new RegExp(key, 'gi'), '<span class="highlightedText">$&</span>')
      }
    })
    return $sce.trustAsHtml(text);
  }
});
.highlightedText {
  background: yellow;
}
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>

<div ng-app="testApp" ng-controller="testCtrl">
  <li ng-repeat="item in data ">
    <span ng-bind-html="item.title | highlight:arrayFilter"></span>
  </li>
</div>


你能帮我解决这个问题吗?https://stackoverflow.com/questions/45929547/highlight-the-text-of-a-span?noredirect=1#comment78817832_45929547 - yavg

0

如果您想突出显示单词之间的空格,这里有一个过滤器版本:

app.filter('highlight', function($sce) { return function(text, arrayFilter) { var regex = "([\\s]*"+arrayFilter.join("[\\s]*)|([\\s]*")+"[\\s]*)"; return $sce.trustAsHtml(text.replace(new RegExp(regex, 'gi'), '<span class="highlightedText">$&</span>')); } });


0

这里是一个可用的过滤器:

app.filter('highlight', function($sce) {
return function(text, arrayFilter) {
    var stringToDisplay = '';
    angular.forEach(arrayFilter,function(key,value){
        if(text.includes(key)){
          text = text.replace(new RegExp(key, 'gi'), '<span class="highlightedText">$&</span>');
        }
    })
   return  $sce.trustAsHtml(text);
}
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接