xss - 防止 xss 攻击,转义特殊字符
使用
js
import { xss } from 'iflyjs'
console.log(xss('<script>alert(1)</script>')) // output: <script>alert(1)</script>
源码
ts
const xss = (str: string): string => {
return str.replace(
/[&<>'"]/g,
(tag) =>
({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag] || tag)
)
}