yavg
yavg

Reputation: 3051

Highlight words from an array

I want to mark the words I have in an array.

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

But it only works for me if I have the words in the order they appear. I wish that regardless of the order in which the words are, these are marked if they match. And if I add a new word to the array it should also be marked.

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

Upvotes: 4

Views: 1878

Answers (3)

Abenitsi
Abenitsi

Reputation: 1

Here is a filter version if you want to highlight white spaces between words too:

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>')); } });

Upvotes: 0

kukkuz
kukkuz

Reputation: 42370

The issue is your are concating your keys - change your filter to this:

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);
  }
});

See updated jsfiddle or demo below:

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>

Upvotes: 6

CCH
CCH

Reputation: 1551

Here is a working filter :

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);
}
});

Upvotes: 0

Related Questions