@plexis/escape-html
Version:
Takes the input text and converts HTML special characters to their entity equivalents.
24 lines (21 loc) • 551 B
JavaScript
/**
* @description Takes the input text and converts HTML special characters to their entity equivalents.
* @param {String} text
* @example
* escapeHTML('ABCD'); // returns 'ABCD'
* escapeHtml('<3') // returns '<3'
* escapeHtml('<p>This is cool</p>') // returns '<p>This is cool</p>'
*/
const escapeHTML = text => {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) {
return map[m];
});
};
export default escapeHTML;