(正则表达式应用) 替换自闭合标签(self-closing tag)的method

var str = "<sup><div class=\"he's\"/></sup><span id=\"cs\"/>";

function removeCloseTag(str) {
    var regCloseTag = /<[^>]+\/>/,
        regTagName = /[^<][^\s]+/,
        tmp,
        tagName;

    while((tmp = str.match(regCloseTag)) && tmp.length > 0){
        tagName = tmp[0].match(regTagName);
        str = str.replace(/\/>/, '></'+tagName+'>');
    }

    return str;
}

var res = removeCloseTag(str); //<sup><div class="he's"></div></sup><span id="cs"></span>
posted @ 2016-05-30 22:42  薄荷君  阅读(1196)  评论(2编辑  收藏  举报