Escape/ Unescape HTML?

Discussion in 'Javascript' started by Philipp, Dec 20, 2007.

  1. Philipp

    Philipp Guest

    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!
     
    Philipp, Dec 20, 2007
    #1
    1. Advertising

  2. Philipp

    Evertjan. Guest

    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)
     
    Evertjan., Dec 20, 2007
    #2
    1. Advertising

  3. 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
     
    Thomas 'PointedEars' Lahn, Dec 21, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SGFycnkgS2Vjaw==?=

    What is the .Net equivalent of jscript escape/unescape?

    =?Utf-8?B?SGFycnkgS2Vjaw==?=, Mar 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,842
    =?Utf-8?B?VGltIFN0YWxs?=
    Mar 9, 2005
  2. Rares Vernica

    unescape HTML entities

    Rares Vernica, Oct 28, 2006, in forum: Python
    Replies:
    4
    Views:
    866
    Klaus Alexander Seistrup
    Nov 1, 2006
  3. NetKev
    Replies:
    1
    Views:
    4,319
    Owen Jacobson
    Mar 4, 2008
  4. Tony Augustine
    Replies:
    0
    Views:
    362
    Tony Augustine
    Jul 22, 2010
  5. RK
    Replies:
    1
    Views:
    135
    Gabriel Gilini
    Nov 12, 2008
Loading...

Share This Page