:::info 用于搜索框中输入搜索并进行内容文本高亮显示 :::
const textShow = (text, keyword) => {
const len = keyword.length
const correctText = keyword
const 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.index
newText += 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