用户的输入用不可信<br>转义输入输出的内容,对于引号、尖括号、斜杠进行转义
function escape(str) {<br> str = str.replace(/&/g, '&')<br> str = str.replace(/</g, '<')<br> str = str.replace(/>/g, '>')<br> str = str.replace(/"/g, '&quto;')<br> str = str.replace(/'/g, ''')<br> str = str.replace(/`/g, '`')<br> str = str.replace(/\//g, '/')<br> return str<br>}
缺点
对于富文本来说,显然不能通过上面的办法来转义所有字符,因为这样会把需要的格式也过滤掉。对<br>于这种情况,通常采用白名单过滤的办法。