:::info 用于搜索框中输入搜索并进行内容文本高亮显示 :::
const textShow = (text, keyword) => {const len = keyword.lengthconst correctText = keywordconst specialStr = ["*", ".", "?", "+", "$", "^", "[", "]", "{", "}", "|", "\\", "(", ")", "/", "%"]let newText = ""if (len !== 0) {let index = 0// specialStr.map(item => {// if (correctText.includes(item)) {// correctText = correctText.replace(new RegExp(`\\${item}`, "g"), `a1s2d3${item}`)// }// })// correctText = correctText.replace(new RegExp("a1s2d3", "g"), `\\`)const matchData = new RegExp(correctText, "i").exec(text)if (matchData) {index = matchData.indexnewText += text.slice(0, index)// class用来定位高亮位置,样式还是以style为准newText += `<span class="heighlight" style='color: #FF3939; background-color: #FFE5E5;'>${text.slice(index, index + len)}</span>`if (text.slice(index + len).length > 0) {newText += textShow(text.slice(index + len), keyword)}} else {newText += text}} else {newText = text}newText = newText.replace(/%5f/g, "_")newText = newText.replace(/%27/g, "'")return newText}export default textShow
