Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to use regexp to replace some html tags in DOM

Reply
Thread Tools

how to use regexp to replace some html tags in DOM

 
 
jiing24@gmail.com
Guest
Posts: n/a
 
      01-26-2006
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/

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      01-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
jiing
Guest
Posts: n/a
 
      01-26-2006
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"

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-26-2006
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
 
Reply With Quote
 
jiing
Guest
Posts: n/a
 
      01-27-2006
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/

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-27-2006
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
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      01-27-2006
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Regexp - global replace of a character between tags MB Javascript 3 03-29-2007 04:04 PM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
RegEx to find CFML tags nested in HTML tags Dean H. Saxe Perl 0 01-03-2004 06:11 PM



Advertisments