473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to use regexp to replace some html tags in DOM

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 '06 #1
6 4485
ji*****@gmail.com 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 26 '06 #2
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"

Jan 26 '06 #3
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
Jan 26 '06 #4
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/

Jan 27 '06 #5
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
Jan 27 '06 #6
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/
Jan 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sandman | last post by:
Textblock (indented for clarity): Hello, my nickname is Sandman, here is a list of things I like: <ol> <li> Perl <li> PHP <li> MySQL </ol>
2
by: Patryk Konieczka | last post by:
Hello Here's the thing I have a database edited by some company workers editing descriptions of books in the sotre , unfortunately these workers do not have the habit of inserting a space...
3
by: DrewM | last post by:
I'm sure this isn't difficult, but it's Friday afternoon (!). I'm trying to use a regular expression to match html tags in a string and convert them to lower case. It's the RegExp object that I'm...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
4
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
20
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "prctico" comes out as "practico" - but ignoring case, so that "PRCTICO" comes out as...
3
by: MB | last post by:
I need to replace all occurances of a certain character located between two tags. I have included an example of what i have come up with so far, but it doesn't work they way I want it to: str =...
1
by: chainspell | last post by:
My very first post here :) I know my way around vb and js but I'm lost with regular expressions.. Anyways I have my function below, and my problem is the function changes all words into links... even...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.