Minor tidy up of String.prototype.htmlEncode and String.prototype.htmlDecode

11 views
Skip to first unread message

Martin Budden

unread,
May 31, 2006, 2:06:53 PM5/31/06
to TiddlyWikiDev
Both these functions would benefit in terms of readability, size and
performance by changing their RegExp strings into regular expression
literals. (Unlike in the matchers there is no strong case for retaining
the RegExp string form.)

(Actually htmlDecode is not used, but it probably should be retained on
symmetry grounds).


// Convert & to "&amp;", < to "&lt;", > to "&gt;" and " to "&quot;"
String.prototype.htmlEncode = function()
{
return(this.replace(/&/mg,"&amp;").replace(/</mg,"&lt;").replace(/>/mg,"&gt;").replace(/\"/mg,"&quot;"));
}

// Convert "&amp;" to &, "&lt;" to <, "&gt;" to > and "&quot;" to "
String.prototype.htmlDecode = function()
{
return(this.replace(/&amp;/mg,"&").replace(/&lt;/mg,"<").replace(/&gt;/mg,">").replace(/&quot;/mg,"\""));
}

Reply all
Reply to author
Forward
0 new messages