xss 转义

export const funEncodeHTML = function (str) {
  if (typeof str === 'string') {
    return str.replace(/<|&|>/g, (matches) => {
      const res = ({
        '<': '&lt;',
        '>': '&gt;',
        '&': '&amp;',
      })[matches];

      return res ?? '';
    });
  }

  return '';
};
posted @ 2022-12-05 20:14  hh9515  阅读(102)  评论(0)    收藏  举报