kun-galgame-vue/src/utils/getPlainText.ts
2023-08-31 17:26:37 +08:00

22 lines
453 B
TypeScript

export function getPlainText(html: string): string {
const re1 = new RegExp('<.+?>', 'g')
const arrEntities: Record<string, string> = {
lt: '<',
gt: '>',
nbsp: ' ',
amp: '&',
quot: '"',
ldquo: '“',
mdash: '—',
rdquo: '”',
}
const plainText = html
.replace(re1, '')
.replace(/&(lt|gt|nbsp|amp|quot|ldquo|mdash|rdquo);/gi, function (all, t) {
return arrEntities[t]
})
return plainText
}