JS转译HTML字符的方法

let escape = (html) => {
  return String(html)
  .replace(/&(?!\w+;)/g, '&')
  .replace(/, '<')
  .replace(/>/g, '>')
  .replace(/"/g, '"')
  .replace(/'/g, ''');
};

let res = escape('')
console.log(res) // <script>console.log(1)</script>

转义就是将能形成HTML标签的字符转换成安全的字符,这些字符主要有 &<>"'

你可能感兴趣的:(基础知识)