html-process.js 850 Bytes
/**
 * html字符处理
 */

'use strict';

const _htmlMap = {'<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;'};
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
};