jquery - Use regex to bold the word in search result which match the search query -


i have stored input text value variable id name=search-query. , searching through json data find matching result , output result on screen. there way can bold word match search-query.val?

<body ng-app="products" ng-controller="ctrl">    <input type="text" id="search-query" name="query" placeholder="enter product name"></input>  <tbody>        <tr ng-repeat="result in searchresult|orderby:order:reverse" >             <td >                <a ng-href="{{result.link}}" target="_blank">                     <span ng-bind="result.name" target="_blank"></span>                  </a>           </td>           <td  ng-bind="result.type"></td>          </tr>    </tbody>  </body>  var app2 = angular.module('products', []); app2.controller('ctrl', function($scope) {   $scope.searchresult = [];  $scope.submit = function(){       var search = $("#search-query").val();     $.each(json.products, function(i, v) {          var regex = new regexp(search, "i");           if (v.name.search(regex) != -1) {                  // following line, there way can bold word match search-query.val?               var name = v.name.replace(search, "<b>search</b>");   // doesn't work                $scope.searchresult.push({ name:name, type: v.type, link: v.url });                return;           }  });    }    

content=document.getelementbyid("content");    content.innerhtml=content.innerhtml.replace(/search/gi,"<b>$&</b>");
<div id="content">this search text</div>

try one

v.name.innerhtml=v.name.innerhtml.replace(/search/gi,"<b>$&</b>"); 

Comments