Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    RegExp/Replace all tags lowercase.

    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.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by ubh View Post
    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:
    Code:
    myString=request.responseText.replace(/(<\/?)([^\s]+)(\s|>)/g, function(a,b,c,d){return b+c.toLowerCase()+d});

  • Users who have thanked Arty Effem for this post:

    ubh (09-09-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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)
    Last edited by mrhoo; 09-09-2008 at 03:27 AM.

  • Users who have thanked mrhoo for this post:

    ubh (09-09-2008)

  • #4
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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.)
    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?

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #8
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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.
    Last edited by ubh; 09-09-2008 at 07:24 AM.

  • #9
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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?

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Something like this may help-
    Code:
    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;
    }
    Last edited by mrhoo; 09-10-2008 at 12:45 AM.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •