Select words from an array

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

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

But this only works for me if I have the words in the order in which they appear. I wish that regardless of the order in which these words 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

+4
source share
3 answers

The problem is that you have strengthened your keys - change filterto 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 jsfiddleor 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>
Run codeHide result
+5
source

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

, :

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

Source: https://habr.com/ru/post/1684613/


All Articles