Skip navigation
LinkMc
Currently Being Moderated

RegEx Replace exact string

Nov 27, 2009 5:09 PM

I am trying to replace an HTML code string. Here is what I have

 

 

public function convertToXHtml(str:String):String {
var pattern:RegExp;
 
// Check for all carriage returns or breaks
pattern = /<p style=".*"><span style=".*"><\/span><\/p>/ig; 
str = str.replace(pattern,"<br />");
 
return str;
}

 

 

 

 

I shortened this down for readability.

 

I have something like this to filter

 

<p style="text-align:left"><span style="font-family:Verdana; font-size:11px; color:#444444;  "></span></p><p style="text-align:left"><span style="font-family:Verdana; font-size:18px; color:#990000;  "><strong>Donec lacinia, metus vitae tristique laoreet</strong></span></p><p style="text-align:left"><span style="font-family:Verdana; font-size:11px; color:#444444;  "></span></p>

 

 

I want my pattern search only to return all empty code strings that have no data so I can replace it with breaks (<br />)

 

<p style="text-align:left"><span style="font-family:Verdana; font-size:11px; color:#444444;  "></span></p><p style="text-align:left"><span style="font-family:Verdana; font-size:11px; color:#444444;  "></span></p>

 

My pattern returns all of it, thus giving me all <br /> even my data.

 

What am i missing?

 
Replies
  • Currently Being Moderated
    Nov 30, 2009 8:09 AM   in reply to LinkMc

    I think the greedy match in .* is getting you. Try replacing .* with [^"]+, and see if that works. It's not quite right (it breaks on embedded escaped double-quotes inside your style attributes), but it should be good enough.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 1, 2009 7:58 AM   in reply to LinkMc

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
        horizontalAlign="center" verticalAlign="middle">
        <mx:Script>
            <![CDATA[
                import mx.formatters.DateFormatter;
               
                private const s:String = '<p style="text-align:left"><span style="font-family:Verdana; font-size:11px; color:#444444;  "></span></p><p style="text-align:left"><span style="font-family:Verdana; font-size:18px; color:#990000;  "><strong>Donec lacinia, metus vitae tristique laoreet</strong></span></p><p style="text-align:left"><span style="font-family:Verdana; font-size:11px; color:#444444;  "></span></p>';
       
                public function convertToXHtml(str:String):String {
                    var pattern:RegExp = /<p style="[^"]*"><span style="[^"]*"><\/span><\/p>/ig;
                    str = str.replace(pattern,"<br />");
                    
                    return str;
                }
       
            ]]>
        </mx:Script>
        <mx:Button label="replace" click="result.text = convertToXHtml(s)"/>
        <mx:HBox>
            <mx:TextArea id="original" width="300" height="400" text="{s}"/>
            <mx:TextArea id="result" width="300" height="400"/>       
        </mx:HBox>
       
    </mx:Application>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points