homepage Welcome to WebmasterWorld Guest from 23.20.77.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Regular Expression Problem
Pattern needed to strip off empty HTML tags
chameleon




msg:1488305
 6:56 pm on Nov 8, 2004 (gmt 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
statement.

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!

 

DrDoc




msg:1488306
 10:15 pm on Nov 9, 2004 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved