programming-examples/js/String/Escape a HTML string.js
2019-11-15 12:59:38 +01:00

17 lines
508 B
JavaScript

function escape_HTML(html_str) {
'use strict';
return html_str.replace(/[&<>"]/g, function (tag) {
var chars_to_replace = {
'&': '&',
'<': '<',
'>': '>',
'"': '"'
};
return chars_to_replace[tag] || tag;
});
}
console.log(escape_HTML('<a href="javascript-string-exercise-17.php" target="_blank">'));
</a>