SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast pentapenguin's Avatar
    Join Date
    Aug 2005
    Location
    GA, USA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Regular Expression Question: Skipping within a Match?

    Hello again.

    Got another fun regexp question. Here's my code:
    HTML Code:
    <html>
    <head>
    <title>JavaScript</title>
    </head>
    <body>
    
    <script>
    var text = 'some text <pre><span style="color: red;">preformatted text</span></pre> more text <pre>more preformatted</pre>';
    var regexp = /<pre>(.*?)<\/pre>/gi;
    
    if ( text.search(regexp) )
    {
    	alert("Match found!");
    	
    	var regexp2 = /<span style="color: ([A-Za-z]{1,10});">(.*?)<\/span>/gi;
    	text = text.replace(regexp2, " COLOR:$1 <br> $2 ");
    	alert(text);
    }
    
    else
    {
    	alert("No match found!");
    }
    </script>
    
    </body>
    </html>
    Here's my text variable I want to use:
    Code:
    var text = 'some text <pre><span style="color: red;">preformatted text</span></pre> more text <pre>more preformatted</pre>';
    I would like to replace
    Code:
    <span style="color: color_here">stuff</span>
    ONLY if the <span> tag IS NOT within a <pre> tag.
    Is this possible? I've tried several different approaches myself and they didn't work.

    Also, I would like the code to be able to do this for an unlimited number of <pre> tags if possible.

    Thank you!!!

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you intend to do with the text after you replace the span elements?

    It seems like it would be easier to replace the span elements via DOM1+ methods if you were doing to add that string to the document with document.write() or innerHTML.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Enthusiast pentapenguin's Avatar
    Join Date
    Aug 2005
    Location
    GA, USA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I should have explained better. This is just an example for a more complicated problem I'm facing. But I agree that normally it would be easier/better to do this through PHP and/or the JS DOM methods, but in this case I need to do it this way.

  4. #4
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Murr ?
    Code:
    <html>
    <head>
    <title>js</title>
    </head>
    <body>
    
    <script>
    var text = 'some text <pre><span style="color: red;">preformatted text</span></pre> more text <pre>more preformatted</pre><span style="color: red;">TEST</span>';
    preElms = text.match(/<pre.+?<\/pre>/gim);
    
    for(var i=0; i<preElms.length; i++){
    	text = text.replace(preElms[i], ('~~~~~~' + i + '~~~~~~'));
    }
    
    var regexp2 = /<span style="color: ([A-Za-z]{1,10});">(.*?)<\/span>/gi;
    text = text.replace(regexp2, " COLOR:$1 <br> $2 ");
    
    for(var i=0; i<preElms.length; i++){
    	text = text.replace(('~~~~~~' + i + '~~~~~~'), preElms[i]);
    }
    
    alert(text);
    
    </script>
    
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •