millermedeiros / amd-utils

modular JavaScript utilities written in the AMD format
http://millermedeiros.github.com/amd-utils
142 stars 11 forks source link

String#escapeHTML & String#unescapeHTML #52

Closed satazor closed 12 years ago

satazor commented 12 years ago

It would be nice to have these two functions because sometimes we do not want to strip tags but escape them instead.

function escapeHTML() {
    return this.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/'/g,"&#39;").replace(/"/g, "&quot;");
}

function unescapeHTML() {
    return this.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&").replace(/&#39;/g,"'").replace(/&quot;/g, '"');
}

A possible implementation just for reference.