Remove empty tags using RegEx
Regex is not for HTML. If you're in JavaScript anyway I'd be encouraged to use jQuery DOM processing.
Something like:
$('*:empty').remove();
Alternatively:
$("*").filter(function()
{
return $.trim($(this).html()).length > 0;
}).remove();
You have "not spaces" as your character class, which means "<i>italic</i></p>
" will match. The first half of your regex will match "<(i>italic</i)>
" and the second half "</(p)>
". (I've used brackets to show what each [\S]+
matches.)
Change this:
/<[\S]+><\/[\S]+>/
To this:
/<[^/>][^>]*><\/[^>]+>/
Overall you should really be using a proper HTML processor, but if you're munging HTML soup this should suffice :)