我们可以使用像这样的javascript函数来中和
function neutralizeText(text) {
var i,mtch=text.match(/(<[a-z]+\s+[^>]*\s+|<[a-z]+\s+)on[a-z]+(\s)*=(\b|\s|[^>])*>/gi);
if(mtch!=null) {
for(i=0;i<mtch.length;i++) {
text=text.replace(mtch[i],mtch[i].replace(/\s+on[a-z]+(\s)*=/gi,' data-neutralized='));
}
}
text=text.replace(/<script(\b|\s)[^>]*>(([ \t\r\n]|.)*?)<\/script>/ig,'');
text=text.replace(/<iframe(\b|\s)[^>]*>(([ \t\r\n]|.)*?)<\/iframe>/ig,'');
text=text.replace(/<object(\b|\s)[^>]*>(([ \t\r\n]|.)*?)<\/object>/ig,'');
text=text.replace(/<\!\-\-(.*?)\-\->/g,'');
text=text.replace(/<\!\-\-/g,'');
return text;
}
但这还不够,您必须在服务器端检查它。因为可以在浏览器中禁用 javascript。