[Project_owners] Re: regex help to lowercase (X)HTML tags needed

Simon Bünzli zeniko at gmail.com
Tue Apr 18 15:13:53 EDT 2006


Neil schrieb am 18.04.06 12:50:
> HJ wrote:
> 
>> need to replace all upper case tags and attributes in XHTML (think
>> XMLHttpRequest) with .replace() but I have no idea *if* that is even
>> possible, or how to make it work (:
> 
> Try responseText.replace(/<\w+\b|\b\w+=/g, String.toLowerCase)

Or somewhat safer (and way more complicated):

responseText.replace(/(<(?![?!])[^\s>]+)(.*?>)/g,
   function(aTag, aTagName, aAttributes) {
      return aTagName.toLowerCase() +
         aAttributes.replace(/(\s\S+=)((["']).*?\3|\S+)/g,
            function(aAttribute, aName, aValue) {
               return aName.toLowerCase() + aValue;
            }
         )
      ;
   }
)
// just don't tell me that a RegExp is no replacement for a parser ;)

If you prefer Neil's version, I'd at least replace \b\w+= with \s\w+= in
order to not convert URL arguments (i.e. HREF="?OrderNo=42" shouldn't
become href=""?orderno=42").

Cheers,
Simon


More information about the Project_owners mailing list