replaceAll方法替换

This commit is contained in:
tangly1024
2022-03-01 17:35:29 +08:00
parent 46925a7597
commit 5bb730d4c2

View File

@@ -60,11 +60,12 @@ export async function getStaticProps ({ params: { keyword } }) {
}
post.results = []
let hit = false
const re = new RegExp(`${keyword}`, 'g')
indexContent.forEach(c => {
const index = c.toLowerCase().indexOf(keyword.toLowerCase())
if (index > -1) {
hit = true
const referText = c?.replaceAll(keyword, `<span class='text-red-500'>${keyword}</span>`)
const referText = c?.replace(re, `<span class='text-red-500'>${keyword}</span>`)
post.results.push(`<span>${referText}</span>`)
} else {
post.results.push(`<span>${c}</span>`)