html-process.js
850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* html字符处理
*/
'use strict';
const _htmlMap = {'<': '<', '>': '>', '&': '&', '"': '"'};
const _EscapeMap = {lt: '<', gt: '>', nbsp: ' ', amp: '&', quot: '"'};
/**
* [移除html标签]
*/
const removeHtml = (str) => {
return (str || '').replace(/<[^>]+>/g, '');
};
/**
* [html转为转义]
*/
const htmlToEscape = (html) => {
return html.replace(/[<>&"]/g, (e) => {
return _htmlMap[e];
});
};
/**
* [转义符转为html]
*/
const escapeToHtml = (str) => {
return str.replace(/&(lt|gt|nbsp|amp|quot);/g, (match, e) => {
return _EscapeMap[e];
});
};
module.exports = {
removeHtml,
htmlToEscape,
escapeToHtml
};