Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Regular Expression Problem

Pattern needed to strip off empty HTML tags

6:56 pm on Nov 8, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:July 9, 2002
votes: 0

Hi All:

I'm trying to find the right Regexp string to remove empty SPAN tags
from an HTML string.

Say I have a string like so, and I want to remove the empty span tags:

<span>This is my text</span>

A simple expression like this /<SPAN>(.*)?<\/SPAN>/gi will give me the
text between the two span tags, which I can then use in a replace

This gets much more complicated when we have nested tags, however.
For example:

<span style="font-weight: bold>one <span>two <span style="color:
red">three</span> four</span> five</span>

What I really want after the replace statement is this:

<span style="font-weight: bold>one two <span style="color:
red">three</span> four five</span>

I'm having trouble crafting the perfect expression for this. I can't
seem to get my head around the right solution to handle the greedy vs
non-greedy thing, and not eliminate the wrong closing tag.

Is this even possible with straight expressions?

Thanks in advance for any help you can provide!

10:15 pm on Nov 9, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
votes: 0

You'd have to use backreferences and lookups... which I'm not entirely sure is supported in JavaScript. Is this for a piece of code that gets submitted through a form? If so, it might be easier to do it on the server side. JS's support for regular expressions isn't the greatest.