Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

html_unescape

Status
Not open for further replies.

tanderso

IS-IT--Management
Aug 9, 2000
981
US
I don't know if anyone else has needed the functionality to output unescaped html from an html-escaped string, but I spent this morning looking in vain on the web for a built-in or custom function to do so. So, after some research on obscure methods, I've built one...
Code:
function html_unescape(s) { return s.replace(/&#(\d{1,3});/g, function($0,$1){return String.fromCharCode(parseInt($1,10)); });}
Hope it helps anyone who needs something similar.

Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Hi Tom,

What problem did you encounter with the builtin functions?

Code:
<html><head><title>TEST</title>
<script language=&quot;JavaScript&quot;>
function escunesc(str){
var escstr = escape(str);
alert(escstr);
var uestr = unescape(escstr);
alert(uestr);
}
</script></head><body>
<textarea rows=&quot;3&quot; cols=&quot;25&quot; onChange=&quot;escunesc(this.value)&quot;>
</textarea></body></html>
 
Those functions only work on URL encoding, not HTML encoding.

Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Eg. this converts

Code:
 & # 34
to a quote

Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top