需求:这里以转义特殊字符串为例
import re

# 替换字符串的映射
map_str = {
    "&": "&",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&#39;",
}

def callback(match):
    """
        返回替换的结果
    """
    return map_str.get(match.group(0))

if __name__ == '__main__':
    src_text = '<script>alert(1)</script>'
    ret = re.compile("[&<>\"']")
    s = ret.sub(callback, src_text)
    print(s)

    """
    输出结果:
        &lt;script&gt;alert(1)&lt;/script&gt;
    """

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2021-07-01
  • 2021-10-12
  • 2021-05-31
  • 2022-01-18
  • 2021-12-12
  • 2021-07-27
  • 2021-12-11
相关资源
相似解决方案