Web Analytics Made Easy -
StatCounter Basic RegExp Help - CodingForum

Announcement

Collapse
No announcement yet.

Basic RegExp Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Basic RegExp Help

    Ok, I can't get my head around regexpressions so I need a bit of help.

    I'm trying to extract the text between two constants, but I'm encountering trouble.

    Here's what the string looks like:
    Code:
    <span class=\"myText\">Random Text In here</span>
    I want the text between the two spans, so I'm using a bit of regex to try and isolate them.

    My regexp looks like this:
    Code:
    var re=/?span class=\"myText\"\>(.+?)\<./;
    Doesn't seem to be right, can anyone help me fix this. Thanks
    Free Web Hosting Funny Christian Jokes
    Fallout Shelter android cheat codes Kritika white knights hacks and cheats

  • #2
    What's the leading question mark for??? Seems out of place.

    And what happens if you encounter something like this:
    Code:
    <span style="width: 300px;" class="myText">look <b>here</b></span>
    ???

    You won't know to skip over the style= and you will terminate on the < of the <b>, no?

    I think you need something like:
    Code:
    var re = /\<span\s.+?class=\"myText\"[^\>]*\>(.+?)\<\/span/
    Be yourself. No one else is as qualified.

    Comment


    • #3
      that didn't work for me unfortunately.

      the string I'm reading looks exactly like this (this is just a demo)
      Code:
      <a class=\"myHeadline\" target=\"_top\" onmouseover=\"window.status=\'http://www.google.com\'; return true;\" onmouseout=\"window.status=\' \'; return true;\" href=\"http://www.google.com/test\"><b><span class=\"myHeadline\">Test Google</span></b></a><br>\n<a class=\"myText\" target=\"_top\" onmouseover=\"window.status=\'http://www.google.com\'; return true;\" onmouseout=\"window.status=\' \'; return true;\" href=\"http://www.google.com\"><span class=\"myText\"> Just a test of the way it works\\t</span></a>
      It's kinda messy, but notice tat the mytext class appears twice - once in an link and once in the span, I'm trying to ignore the link and just get it from the span. I managed to get the link (href) using this:
      Code:
      +?href=\"(.+?)\".
      and the main headline:
      Code:
      +?\"myHeadline\"\>(.+?)\<.
      all together it looks like this, but the last part is what is getting me confused, and unfortunately it's not working.
      Code:
      var re = /window.status=\'(.+?)\'.+?href=\"(.+?)\".+?\"myHeadline\"\>(.+?)\<.+?span class=\"myText\"\>(.+?)\<./;
      Last edited by BatCountry; Apr 5, 2009, 11:37 PM.
      Free Web Hosting Funny Christian Jokes
      Fallout Shelter android cheat codes Kritika white knights hacks and cheats

      Comment


      • #4
        This may move you forward:-

        Code:
        <script type = "text/javascript">
        
        var string = "<a class=\"myHeadline\" target=\"_top\" onmouseover=\"window.status=\'http://www.google.com\'; return true;\" onmouseout=\"window.status=\' \'; return true;\" href=\"http://www.google.com/test\"><b><span class=\"myHeadline\">Test Google</span></b></a><br>\n<a class=\"myText\" target=\"_top\" onmouseover=\"window.status=\'http://www.google.com\'; return true;\" onmouseout=\"window.status=\' \'; return true;\" href=\"http://www.google.com\"><span class=\"myText\"> Just a test of the way it works</span></a>"
        
        string1 = string.match(/<span.+span>/g).toString();
        string1 = string1.replace(/(<span.+">)/g,"");
        string1 = string1.replace(/(<\/span>)/g,"");
        
        alert(string1);  // [COLOR="Blue"]Just a test of the way it works[/COLOR]
        
        </script>
        IE is sometimes a little peculiar, and you have to take two bites at the cherry.


        Quizmaster: The Garden of Gethsemane in Jerusalem is at the foot of the Mount of ... what?
        Contestant: Everest.
        Last edited by Philip M; Apr 6, 2009, 07:40 AM. Reason: Works OK with <b> </b>

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          Umm...Philip, he only wanted spans where the class="myText".

          And what about if there are more than one match in the text???

          Try this:
          Code:
          <script>
          var test = "<a class=\"myHeadline\" target=\"_top\" onmouseover=\"window.status=\'http://www.google.com\'; return true;\" " 
                   + " onmouseout=\"window.status=\' \'; return true;\" href=\"http://www.google.com/test\"><b><span class=\"myHeadline\">"
                   + "Test Google</span></b></a><br>\n<a class=\"myText\" target=\"_top\" "
                   + " onmouseover=\"window.status=\'http://www.google.com\'; return true;\" "
                   + " onmouseout=\"window.status=\' \'; return true;\" href=\"http://www.google.com\">"
                   + "<span class=\"myText\"> Just a test of the way it works</span></a>"
                   + "<br><br>More stuff and then <SPAN style=\"color: red;\" class=\"MYTEXT\">Zambonis!</Span>"
          
          var re =  /\<span\s.*?class=\"myText\".*?\>(.+?)\<\/span/ig
          
          document.write( test + "<br><br><hr>" );
          
          var aMatch;
          while ( (aMatch = re.exec(test)) != null )
          {
              document.write( aMatch[1] + "<HR>" );
          }
          </script>
          I added an extra <span class="myText"> and as you can see I also added a style= to it and the re properly skips over it.

          Works identically in MSIE and FF.
          Be yourself. No one else is as qualified.

          Comment

          Working...
          X