Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Escape/ Unescape HTML? (http://www.velocityreviews.com/forums/t933996-escape-unescape-html.html)

Philipp 12-20-2007 07:52 PM

Escape/ Unescape HTML?
 
How do I convert a string to escaped HTML? (Let's say I'm not using
DOM.) E.g. "<p>foo</p>" would become "&lt;p&gt;foo&lt;/p&gt;"?

Reversely, how do I unescape a string to be HTML? E.g. "&lt;em&gt;"
would turn back into "<em>".

I mean a function like the following would do the job but is there a
more JS-native solution?

html = html.replace(/&/g, "&amp;");
html = html.replace(/</g, "&lt;");
html = html.replace(/>/g, "&gt;");
html = html.replace(/"/g, "&quot;");

Thanks!

Evertjan. 12-20-2007 10:05 PM

Re: Escape/ Unescape HTML?
 
Philipp wrote on 20 dec 2007 in comp.lang.javascript:

> How do I convert a string to escaped HTML? (Let's say I'm not using
> DOM.) E.g. "<p>foo</p>" would become "&lt;p&gt;foo&lt;/p&gt;"?
>
> Reversely, how do I unescape a string to be HTML? E.g. "&lt;em&gt;"
> would turn back into "<em>".


> I mean a function like the following would do the job but is there a
> more JS-native solution?


Ever tried asking the natives? Oh you just did. ;-)

s = unescape(s);

the below is not unescape, but escape-ish:

> html = html.replace(/&/g, "&amp;");
> html = html.replace(/</g, "&lt;");
> html = html.replace(/>/g, "&gt;");
> html = html.replace(/"/g, "&quot;");


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Thomas 'PointedEars' Lahn 12-21-2007 08:07 PM

Re: Escape/ Unescape HTML?
 
Philipp wrote:
> How do I convert a string to escaped HTML? (Let's say I'm not using
> DOM.) E.g. "<p>foo</p>" would become "&lt;p&gt;foo&lt;/p&gt;"?
>
> Reversely, how do I unescape a string to be HTML? E.g. "&lt;em&gt;"
> would turn back into "<em>".
>
> I mean a function like the following would do the job but is there a
> more JS-native solution?
>
> html = html.replace(/&/g, "&amp;");
> html = html.replace(/</g, "&lt;");
> html = html.replace(/>/g, "&gt;");
> html = html.replace(/"/g, "&quot;");


`"' does not need to be escaped, however if this is what you are looking
for, then

function escapeHTML(s)
{
return s.replace(
/[&<>"]/g,
function(m)
{
var map = {
"&": "amp",
"<": "lt",
">": "gt",
'"': "quot"
};

return "&" + map[m] + ";";
});
}

and

function unescapeHTML()
{
return s.replace(
/&(amp|[lg]t|quot);/g,
function(m, p1)
{
var map = {
amp: "&",
lt: "<",
gt: ">",
quot: '"'
};

return map[p1];
});
}

may very well be the answer.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


All times are GMT. The time now is 12:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.