htmlHighlightFn(content, keyword){ if(content) { if(keyword){ let commonUUId = 'htmlHighlight202203171112' let regex = /<img.*?(?:>|\/>)/g let imgArr = content.match(regex) let replaceHtml = content.replace(regex,commonUUId) let mainHtml = replaceHtml.split(commonUUId) let returnContentArr = [] let num = 0 let htmlContent = '' if(Array.isArray(keyword)) { mainHtml.forEach(ele=>{ let beforeContent = ele if(ele) { keyword.forEach(item => { let newContent = beforeContent.split(item) let lastContent = newContent.join(`<span style='color:red'>${item}</span>`) beforeContent = lastContent }) returnContentArr.push(beforeContent) }else { returnContentArr.push('') } }) }else { mainHtml.forEach(ele=>{ let newContent = ele.split(keyword) let lastContent = newContent.join(`<span style='color:red'>${keyword}</span>`) returnContentArr.push(lastContent) }) } returnContentArr.forEach((item,index)=>{ if(index === 0 && !item) { num=1 } if(item) { if(ImgArr) { htmlContent += imgArr[index - num] + item }else { htmlContent += item } } }) return htmlContent }else { return content } } }
|