UNPKG

tyro-util

Version:
22 lines (20 loc) 439 B
/** * @desc 转义HTML(防XSS攻击) * @param {String} str * * eg. * escapeHTML('<a href="#">Me & you</a>'); // '&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;' */ const escapeHTML = str => str.replace( /[&<>'"]/g, tag => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', "'": '&#39;', '"': '&quot;', }[tag] || tag) ) module.exports = escapeHTML