mark search string dynamically using angular.js

前端 未结 4 2134
北荒
北荒 2021-02-08 07:22

How can I mark my search pattern dynamically in my html?
Example:

\"SearchExample\"

I\'m using angular

4条回答
  •  星月不相逢
    2021-02-08 08:05

    Inspired by @tungd's answer but valid for multiple search terms.

    .filter('highlight', function($sce) {
      return function(text, phrase) {
        if (phrase){
            phrases = phrase.split(" ");
            for(i=0;i');
            text = text.replace(new RegExp('('+'%%%%%'+')', 'gi'),'')      
        } 
        return $sce.trustAsHtml(text)
      }
    });
    

    PS: One can always limit the input to be in non-special chars for this to be 100% bullet-proof.

提交回复
热议问题