kun-galgame-vue/src/utils/getPlainText.ts

22 lines
453 B
TypeScript
Raw Normal View History

2023-08-24 11:51:35 +00:00
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
}