正则表达式 处理新闻列表关键词高亮
/** * 处理新闻列表关键词高亮
* newsList-新闻列表数组 searchValue-搜索词,即需要高亮的字符 */ function setHighlight(newsList) { if (searchValue != '') { var arr = newsList; var newArr = []; var s = searchValue; var reg = new RegExp("(" + s + ")", "g"); console.log(s); for(var i =0; i< arr.length; i++){ var newstr = arr[i].TitleCN.replace(reg, "<span style='color:red'>$1</span>"); newArr.push({ TitleCN: newstr, SourceUrl: arr[i].SourceUrl }); } var htmlStr = ''; for(var i=0; i<newArr.length; i++) { htmlStr += '<p class="result-news-p"><i class="result-news-circle"></i><a class="result-news-one" href="'+newArr[i].SourceUrl+'" target="_blank">'+newArr[i].TitleCN+'</a></p>'; } $('#result-news-list').html(htmlStr); } }
本文来自博客园,作者:秋风2016,转载请注明原文链接:https://www.cnblogs.com/lml2017/p/10607246.html