how to use regexp to replace some html tags in DOM

Discussion in 'Javascript' started by jiing24@gmail.com, Jan 26, 2006.

  1. Guest

    I try to use regexp to replace some html tags in DOM, but the result
    seems some problems,


    ================================
    <Script language="javascript" type="text/javascript">

    var config = document.getElementById("rootconfig");

    alert(config.innerHTML);

    regexp=new RegExp("CHECKED","g");


    config.innerHTML=config.innerHTML.replace(regexp,"\Lchecked=\"checked\"");

    regexp2= new RegExp("selected","g");


    config.innerHTML=config.innerHTML.replace(regexp2,"\Lselected=\"selected\"");

    alert(config.innerHTML);

    xmlhttp.open ("POST", "http://100.0.0.1/cgi-bin/Upload.cgi",
    false);

    xmlhttp.send(config.innerHTML);

    alert(xmlhttp.responseText);

    }
    </script>
    <div id="rootconfig"><h1>hello</h1><h1>hi</h1><input type="checkbox"
    checked="checked">abc </input><select><option>123</option><option
    value="456" selected="selected">456</option></select> </div>
    <input type="button" onclick="UpdateXML()" value="UpdateXML"></input>
    ================================

    My result is:
    If I use IE:
    <H1>hello</H1>^M
    <H1>hi</H1><INPUT type=checkbox Lchecked="checked">abc
    </INPUT><SELECT><OPTION selected>123</OPTION><OPTION value=456
    Lselected="selected">456</OPTION></SELECT>
    ^^^^
    There is a unnecessary "L" in the text

    If I use firefox:
    <H1>hello</H1>^M
    <H1>hi</H1><INPUT type=checkbox Lchecked="checked">abc
    </INPUT><SELECT><OPTION selected>123</OPTION><OPTION value=456
    lselected="selected">456</OPTION></SELECT>
    ^^^^

    and in firefox:
    there is some duplicate
    <H1>hello</H1>^M
    <H1>hi</H1><INPUT type=checkbox Lchecked="checked">abc
    </INPUT><SELECT><OPTION selected
    Lselected="selected">123</OPTION><OPTION

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    value=456>456</OPTION></SELECT>

    ================================

    Could anyone help me?
    Thanks in advanced.

    /jiing/
    , Jan 26, 2006
    #1
    1. Advertising

  2. writes:

    > I try to use regexp to replace some html tags in DOM, but the result
    > seems some problems,


    ....
    > config.innerHTML=config.innerHTML.replace(regexp,"\Lchecked=\"checked\"");


    "\L" is not a recognized escape in Javascript string literals, so it will
    just become "L".


    > Lselected="selected">456</OPTION></SELECT>
    > ^^^^
    > There is a unnecessary "L" in the text


    Hardly surprising given the above :)

    What did you expect "\L" to do?
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 26, 2006
    #2
    1. Advertising

  3. jiing Guest

    That's strange if no \L or no L
    my result will the same as the original, but I want to replace CHECKED
    with checked="checked" and selected with selected="selected"
    jiing, Jan 26, 2006
    #3
  4. jiing wrote:

    > That's strange if no \L or no L


    Pardon?

    <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
    <URL:http://www.safalra.com/special/googlegroupsreply/>

    > my result will the same as the original, but I want to replace CHECKED
    > with checked="checked" and selected with selected="selected"


    Try this:

    var config = {
    innerHTML:
    'Please leave checkboxes for selected materials checked:\n'
    + '<select multiple>\n'
    + '<option selected>This option is selected</option>\n'
    + '<option SELECTED>This option is also selected</option>\n'
    + '<option>This option is not selected</option>\n'
    + '</select>\n'
    + '<input type="checkbox" value="bar" checked>\n'
    + '<input type="checkbox" CHECKED value="42">\n'
    + '1 < select is not a select element, therefore selected and checked'
    + ' stay as they are.>\n'
    + '<!-- This is not an element, therefore selected and checked stay'
    + ' as they are. -->'
    };

    config.innerHTML = config.innerHTML.replace(
    /(<\w[^>]*\b)(checked|selected)([^>]*>)/gi,
    function(match, p1, p2, p3)
    {
    var p2 = p2.toLowerCase();
    return p1 + p2 + '="' + p2 + '"' + p3;
    });

    window.alert(config.innerHTML);

    Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
    be Valid and is likely not to work. Also note that there are more boolean
    attributes in HTML than just `checked' and `selected'. I really wonder
    why you are attempting this in the first place.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 26, 2006
    #4
  5. jiing Guest

    Thomas 'PointedEars' Lahn 寫é“:

    > Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
    > be Valid and is likely not to work. Also note that there are more boolean
    > attributes in HTML than just `checked' and `selected'. I really wonder
    > why you are attempting this in the first place.


    > PointedEars


    Hi, PointerdEars

    My goal is to make the IE's DOM to fit the W3C standard
    for example, IE's DOM will let
    <input type="checkbox" CHECKED> rather than
    <input type="checkbox" checked="checked"> //W3C standard

    Do you know any good method to transfer IE's DOM to W3C standard.
    Although I am a newbie in javascript, but I can see that your code is
    very beautiful.
    Thanks a lot.

    /jiing/
    jiing, Jan 27, 2006
    #5
  6. jiing wrote:

    > Thomas 'PointedEars' Lahn ???
    >> Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
    >> be Valid and is likely not to work. Also note that there are more
    >> boolean attributes in HTML than just `checked' and `selected'. I really
    >> wonder why you are attempting this in the first place.
    >> [...]


    Please do not quote signatures unless you are referring directly to them.

    > [...]
    > My goal is to make the IE's DOM to fit the W3C standard
    > for example, IE's DOM will let
    > <input type="checkbox" CHECKED> rather than
    > <input type="checkbox" checked="checked"> //W3C standard


    How did you get that idea? The former is Valid HTML which is an SGML
    application; the latter is _almost_ Valid XHTML which is an XML application
    and must therefore additionally adhere to XML well-formedness (XML is a
    more strictly defined subset of SGML). Likewise for `selected' and other
    boolean attributes. HTML 4.01 is a W3C Recommendation -- and therefore
    considered a (World Wide) _Web_ (W3) standard; W3C is not a pure
    standardization organization -- as are XHTML 1.0 and XHTML 1.1 to date.
    Do not fall for the XHTML hype, see below.

    <URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>
    <URL:http://www.w3.org/TR/xhtml1/#C_10>
    <URL:http://validator.w3.org/>

    > Do you know any good method to transfer IE's DOM to W3C standard.


    There is none reasonable. Are you misguidedly serving XHTML as text/_html_
    to IE and now are wondering why .inner_HTML_ reads _HTML_ and not XHTML?
    Do not serve it as text/html but as application/xhtml+xml (and IE will
    eventually fail to display what it in fact does not support at all), and
    do use an XML serializer then, or serve _HTML_ as text/html instead.

    <URL:http://hixie.ch/advocacy/xhtml>

    > Although I am a newbie in javascript, but I can see that your code is
    > very beautiful.


    Thanks.

    > Thanks a lot.


    You're welcome.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 27, 2006
    #6
  7. Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 1/26/2006 10:46 PM:
    > jiing wrote:
    >
    >> Thomas 'PointedEars' Lahn ???
    >>> Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
    >>> be Valid and is likely not to work. Also note that there are more
    >>> boolean attributes in HTML than just `checked' and `selected'. I really
    >>> wonder why you are attempting this in the first place.
    >>> [...]

    >
    > Please do not quote signatures unless you are referring directly to them.


    There is no "signature" in your post according to the specs you like to
    quote so much. A "proper signature" is delimited by <dash><dash><space>
    and since your post does not contain that sequence of characters then
    the only sensible conclusion is that your post has no signature. So, how
    does one quote something that doesn't exist?

    If you don't want your "signature" quoted, then please delimit it
    properly. Thanks.

    >> [...]
    >> My goal is to make the IE's DOM to fit the W3C standard
    >> for example, IE's DOM will let
    >> <input type="checkbox" CHECKED> rather than
    >> <input type="checkbox" checked="checked"> //W3C standard

    >
    > How did you get that idea? The former is Valid HTML which is an SGML
    > application;


    Both are Valid HTML 4.01 Strict.

    > Do not fall for the XHTML hype, see below.
    >
    > <URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>
    > <URL:http://www.w3.org/TR/xhtml1/#C_10>
    > <URL:http://validator.w3.org/>
    >
    >> Do you know any good method to transfer IE's DOM to W3C standard.

    >
    > There is none reasonable. Are you misguidedly serving XHTML as text/_html_
    > to IE and now are wondering why .inner_HTML_ reads _HTML_ and not XHTML?
    > Do not serve it as text/html but as application/xhtml+xml (and IE will
    > eventually fail to display what it in fact does not support at all), and
    > do use an XML serializer then, or serve _HTML_ as text/html instead.


    A better alternative is to simply server it as text/html with an
    appropriate DTD and not fall for the temptation of XHTML no matter who
    hypes it. At least until some version of IE supports it. Until then, it
    is not worth the effort to code to XHTML standards.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jan 27, 2006
    #7
    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. Dean H. Saxe
    Replies:
    0
    Views:
    1,015
    Dean H. Saxe
    Jan 3, 2004
  2. Rob Nicholson
    Replies:
    3
    Views:
    681
    Rob Nicholson
    May 28, 2005
  3. Mike
    Replies:
    3
    Views:
    863
    Michael Borgwardt
    Jan 9, 2004
  4. Joao Silva
    Replies:
    16
    Views:
    342
    7stud --
    Aug 21, 2009
  5. MB
    Replies:
    3
    Views:
    106
    Evertjan.
    Mar 29, 2007
Loading...

Share This Page