jQuery replace tag

Replace tag saving attributes

HTML

<span>It is 
    <u style="color:red;">Sample</u>
</span>

<span style="color:green;">Text</span>

JavaScript

jQuery.fn.changeTag = function (newTag) {
    var q = this;
    this.each(function (i, el) {
        var h = "<" + el.outerHTML.replace(/(^<\w+|\w+>$)/g, newTag) + ">";
        try {
            el.outerHTML = h;
        } catch (e) { //elem not in dom
            q[i] = jQuery(h)[0];
        }

    });
    return this;
};
$("span").changeTag("p");
$("p").last().clone().changeTag("b").prependTo("body")