...

View Full Version : RegExp/Replace all tags lowercase.



ubh
09-09-2008, 02:30 AM
Anyone ever seen a RegExp/replace that will make all tags lowercase?

I would use the toLowerCase(), but this makes the tags/attribute/content between tags all lowercase. I just need the tags to be lower case inorder to fix IE's problem of uppercase all tags in a document when using DOM and Ajax HTTPRequests.


Just when you thought IE couldn't get anymore evil.. it just did.

Arty Effem
09-09-2008, 03:52 AM
Anyone ever seen a RegExp/replace that will make all tags lowercase?

I would use the toLowerCase(), but this makes the tags/attribute/content between tags all lowercase. I just need the tags to be lower case inorder to fix IE's problem of uppercase all tags in a document when using DOM and Ajax HTTPRequests.
I just threw this together so I don't know how robust it is, but it may be adequate for your needs:

myString=request.responseText.replace(/(<\/?)([^\s]+)(\s|>)/g, function(a,b,c,d){return b+c.toLowerCase()+d});

mrhoo
09-09-2008, 04:07 AM
To just replace tagnames you can use this, where s is the html string:
s= s.replace(/< *\/?(\w+)/g,function(w){return w.toLowerCase()});

//test:
var s='<HEAD><TITLE>something</TITLE></HEAD><BODY><P>Something</P></BODY>';
s= s.replace(/< *(\/ *)?(\w+)/g,function(w){return w.toLowerCase()});
alert(s)

ubh
09-09-2008, 06:48 AM
Arty Effem and mrhoo thank you both for helping me.
mrhoo your solution worked perfect for my situation.

but, lol (isnt there always a but..) I have one last issue in this area that I just noticed that IE is causing again.

take your test mrhoo (NOTICE: I added a id of TestId to the paragraph tag.)

var s='<HEAD><TITLE>something</TITLE></HEAD><BODY><P id="TestId">Something</P></BODY>';
s= s.replace(/< *(\/ *)?(\w+)/g,function(w){return w.toLowerCase()});
alert(s)

If you run it you will notice that the id="TestId" becomes id=TestId without quotes... Is IE compleatly against XHTML or what?

Is there any way to fix this issue at all?

rnd me
09-09-2008, 06:56 AM
use the regexp i posted in response to your post on webdeveloper forum. it was stolen from the ecmascript specification, so you know it's good.

ubh
09-09-2008, 07:12 AM
Hey rnd me,
I just tried that RegExp it douse make my tag in my string lowercase but it also lowercase's attributes as well. But my question of the XHTML standard quotations for attributes being preserved is still up in the air.

Thanks m8.

rnd me
09-09-2008, 07:30 AM
hmm. sorry about that.
i'm still a little confused though.

are you changing the response from ajax, or does the string originate from something.innerHTML ?

if its from ajax, you likely don't need to fix anything; it should work even with uppercase tags. the dynamic info doesn't need to validate, so it should not matter, though it might in xhtml strict i guess...

ubh
09-09-2008, 08:08 AM
Hey rnd me,

The over all process of my application is:


loads an external .html files source via AJAX and passes it into my applications edit area called pallet(div box).

Once my pallet(div box) has received my external files source I then pass the source of my pallet into a textarea.

Via DOM the client can now add/remove/edit html elments in my pallet(div box).

once a DOM edit has been preformed my textarea gets updated with the pallets new source (here is where I now do a "scrub" and lowercase the string of html from pallet via innerHTML before my textarea gets updated THANK YOU IE!!).


AJAX/PHP then posts the value of my textarea back out to the file it originally came from..


(Reason for using a middle man textarea is to allow users to eiter use DOM to make edits or edit the source by hand. Kinda important in the overall project.)

so far everything has been working perfect in all browser except IE and yes it still works as it should I am just being very strict of my output being XHTML standard.

<Catches breath lol

So begin able to preserve tag attribute quotes after converting a string of html tags to being lower case is kind of the last part of the puzzle lol.

ubh
09-09-2008, 10:03 PM
BUMP Previous Message

mrhoo your solution worked perfect for my situation.

but, lol (isnt there always a but..) I have one last issue in this area that I just noticed that IE is causing again.

take your test mrhoo (NOTICE: I added a id of TestId to the paragraph tag.)
Code:

var s='<HEAD><TITLE>something</TITLE></HEAD><BODY><P id="TestId">Something</P></BODY>';
s= s.replace(/< *(\/ *)?(\w+)/g,function(w){return w.toLowerCase()});
alert(s)

If you run it you will notice that the id="TestId" becomes id=TestId without quotes... Is IE compleatly against XHTML or what?

Is there any way to fix this issue at all?

mrhoo
09-10-2008, 01:41 AM
Something like this may help-

function reformHTML(s){
s= s.replace(/>\s*< */g,'>\n<').replace(/\/ *>/g,'>');
s= s.replace(/< *\/?(\w+)/g,function(c){
return c.toLowerCase()
});
s= s.replace(/\n< *(head|\/?body|div|h\d|p|fieldset|form|blockquote|ul|ol|dl|table\/html)/g,'\n\n<$1');
var Rx=/<\w+[^>]+>/g;
var pat,tem='',temp= '',str= '',x= 0;
while((pat= Rx.exec(s)) != null){
tem+= s.substring(x,pat.index);
temp=pat[0];
if(/; *charset *=/i.test(temp)==false){
temp= temp.replace(/\= *([^"\s]+(?=[ >]))/g,'=\"$1\"');
}
temp= temp.replace(/[\w-]+ *\= */g,function(c){
return c.toLowerCase()
});
temp=temp.replace(/style *\= *"[^"]+/g,function(c){
return c.toLowerCase()
});
tem+= temp;
x= Rx.lastIndex;
}
tem+= s.substring(x);
return tem;
}

rnd me
09-10-2008, 03:00 AM
i see. this is why my editor uses only a textarea and hand-coding, to avoid DOM inconsistencies.

if mr hoo's recomendation doesn't work, a worst case scenario could be to make an array with all valid tag names, and loop though them replacing "<XXX" with "<xxx" and also "XXX>" with "xxx>".

that almost has to work.

you can rip off a tag list from w3schools.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum