🔰 JS ⟩ Types ⟩ String ⟩ methods
💾 程式:codepen ⟩ escapeHTML() ⭐️
// ⭐️ escapeHTML()
export function escapeHTML(str){
return new Option(str).innerHTML;
}
// ⭐️ str.escapeHTML()
String.prototype.escapeHTML = function(){
return escapeHTML(this);
}
// ⭐️ escape HTML special characters (deprecated❗️)
String.prototype.escapeHTML = function () {
return this
.replace(/&/g, '&' ) // ampersand (must be first❗️)
.replace(/>/g, '>' ) // greater than
.replace(/</g, '<' ) // less than
.replace(/"/g, '"') // double-quote
.replace(/'/g, ''' ) // single-quote
.replace(/`/g, '`' ); // backtick
}
Document ⟩ .createTextNode() - creates a new Text
node. This method can be used to escape HTML characters.