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>