4 Replies Latest reply on Dec 1, 2009 7:58 AM by msakrejda

    RegEx Replace exact string

    LinkMc Level 1

      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?

        • 1. Re: RegEx Replace exact string
          LinkMc Level 1

          I hate to do this, but I really need help on this one....

           

          Bump..

          • 2. Re: RegEx Replace exact string
            msakrejda Level 4

            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.

            • 3. Re: RegEx Replace exact string
              LinkMc Level 1

              Nope... tried it, I really can't believe it's that hard, but I am not really good at regular expressions.

               

              Thanks for the try  though, I really appreciate your help.

               

              Link

              • 4. Re: RegEx Replace exact string
                msakrejda Level 4

                <?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>