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

Victor has posted 16 posts at DZone. View Full User Profile

TagsToLowerCase

02.06.2006
| 334 views |
  • submit to reddit
        // From shauninman.com  - Given an HTML string with uppercase tags and attributes and mixed case attribute values and tag contents, return that string with lowercased tags and attributes but leave the attribute values and tag contents alone. (Certain browsers return the innerHTML of elements in this malformed way—which can be a problem if the source is headed to a textarea and ultimately saved to a database.)

function tagsToLowerCase(html)
{
    html = html.replace(/([a-z])\s*(=)\s*("|')/gi, '$1$2$3');
    var parts = html.match(/(<\/?[a-z][a-z0-9]*| [a-z]+=)/gi);
    for (var i = 0; i < parts.length; i++)
    {
        var part = parts[i];
        html = html.replace(new RegExp(part, 'g'), part.toLowerCase());
    }
    return html;
}