DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

ToStandard //JavaScript Function

02.14.2006
| 1463 views |
  • submit to reddit
        This function tries to parse bad html and transform into xhtml, by lower-casing the tags, properties and duplicating single properties (readonly becomes readonly="readonly"), my function is very generic.

example:
s = '<INPUT type=text value="a b c" readonly x="123">blabla<DIV></DIV>';
prompt("", toStandard(s));

code:
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

function toStandard(s){
	return s.replace(/<(\/?\w+)([^>]*)>/g, function(s, t, c, d){
		return "<" + t.toLowerCase() + c.replace(/\b(\w+)(?:=((?:(?=(["'])).((?!\3)(?:.|\n))*\3)|\S*))?/g, function(s, p, v, a){
			return (p = p.toLowerCase()) + "=" + (a = a ? "" : '"') + (v || p) + a;
		}) + ">";
	});
};