@cainiaofe/cn-utils
Version:
菜鸟前端基础工具库
41 lines (40 loc) • 921 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.htmlDecode = exports.htmlEncode = void 0;
/**
* HTML转义
* @param {String} html HTML代码
*/
var htmlEncode = function (html) {
// < -> <
html = html + '';
return html.replace(/[<>&"]/g, function (res) {
return {
'<': '<',
'>': '>',
' ': ' ',
'&': '&',
'"': '"',
}[res];
});
};
exports.htmlEncode = htmlEncode;
/**
* HTML反转义
* @param {String} html HTML代码
*/
var htmlDecode = function (html) {
// < -> <
html = html + '';
var arrEntities = {
lt: '<',
gt: '>',
nbsp: ' ',
amp: '&',
quot: '"',
};
return html.replace(/&(lt|gt|nbsp|amp|quot);/gi, function (all, t) {
return arrEntities[t];
});
};
exports.htmlDecode = htmlDecode;