2

I need to parse an HTML string and remove all the elements which contain only empty children.

Example:

<P ALIGN="left"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="1"><B></B></FONT></P>

contains no information and must be replaced with </br>

I wrote a regex like this:

<\w+\b[^>]*>(<\w+\b[^>]*>\s*</\w*\s*>)*</\w*\s*>

but the problem is that it's catching only 2 levels of the three. In the abobe example, the <p> element (the outer-most one) is not selected.

Can you help me fix this regex?

4
  • 1
    brace yourself for downvotes on regex+HTML question Commented Nov 13, 2013 at 10:29
  • 3
    The font element has been deprecated since HTML3 so why are you still using it? Commented Nov 13, 2013 at 10:30
  • stackoverflow.com/q/3129738/612202 You should prefer the answer with more votes. Commented Nov 13, 2013 at 10:30
  • this is the point, I want to get rid of it. I have an older database from where I take this info. There are some notes with formatting saved as text and I want to get rid off useless elements and of font elements. I replaced them with spans Commented Nov 13, 2013 at 10:50

3 Answers 3

5

This regex seems to work:

/(<(?!\/)[^>]+>)+(<\/[^>]+>)+/

See a live demo with your example.

Sign up to request clarification or add additional context in comments.

Comments

2

Use jQuery and parse all children. For each child you have to check if .html() is empty. If yes -> delete the current element (or the parent if you want) with .remove().

Do for each string:

var appended = $('.yourparent').append('YOUR HTML STRING');

appended.children().each(function () 
{
    if(this.html() === '')
    {
        this.parent().remove(); 
    }
});

This will add the items first and delete, if there are empty children.

Comments

0

please try this:

function removeEmtpyElements(str, iterations){
    var re = /<([A-z]+)([^>^/]*)>\s*<\/\1>/gim;
    var subst = '';
    
    for(var i = 0; i < iterations; i++){
        str = str.replace(re, subst);
    }
    
    return str;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.