remove the content in between tags

Discussion in 'ASP General' started by j1c, Mar 15, 2005.

  1. j1c

    j1c Guest

    How can I remove the content in between tags? I have a page that has
    several custom tags: <!--tag:1--> Content 1 <!--/tag:1--> <br>
    <!--tag:2--> Content 2 <!--/tag:2--> <br> <!--tag:3--> Content 3
    <!--/tag:3--> If I only wanted to see the contents of tag 2 for
    example, how could I strip out 1 and 3?
     
    j1c, Mar 15, 2005
    #1
    1. Advertising

  2. j1c

    j1c Guest

    Could you give an example?
     
    j1c, Mar 15, 2005
    #2
    1. Advertising

  3. j1c

    McKirahan Guest

    "j1c" <> wrote in message
    news:...
    > How can I remove the content in between tags? I have a page that has
    > several custom tags: <!--tag:1--> Content 1 <!--/tag:1--> <br>
    > <!--tag:2--> Content 2 <!--/tag:2--> <br> <!--tag:3--> Content 3
    > <!--/tag:3--> If I only wanted to see the contents of tag 2 for
    > example, how could I strip out 1 and 3?
    >


    Can you change
    <!--tag:1--> Content 1 <!--/tag:1-->

    to
    <span id="tag1"> Content 1 </span>

    if so then will the following help? Watch for word-wrap.

    <html>
    <head>
    <title>tags.htm</title>
    <script type="text/javascript">
    function tags(what) {
    document.getElementById("tag"+what).style.visibility = "hidden";
    }
    </script>
    </head>
    <body>
    <span id="tag1"> Content 1 </span>
    <br>
    <span id="tag2"> Content 2 </span>
    <br>
    <span id="tag3"> Content 3 </span>
    <input type="button" value="Hide 1" onclick="tags(1)">
    <input type="button" value="Hide 2" onclick="tags(2)">
    <input type="button" value="Hide 3" onclick="tags(3)">
    </body>
    </html>
     
    McKirahan, Mar 15, 2005
    #3
  4. j1c

    j1c Guest

    I used a regex to do it, however I need to do a 'reverse' loop? :)

    So, if I have 10 section tags, but only want to see 1,5,7 how can I
    loop through removing any matches that do not equal 1,5 or 7
     
    j1c, Mar 15, 2005
    #4
  5. j1c

    j1c Guest

    ;) yeah it is..

    I am getting passed a large string of HTML that will be displayed on a
    site or in an email. This string has up to 10 openning and closing tags
    like:
    <!--tag:1--> bla bla bla <!--/tag:1-->

    I am also passed a recordset that has a delimitted list of params
    (1,5,7)

    Those params match tags that are to be displayed. The rest are to be
    stripped.

    My regex's are a little rusty, but can I not match everything that !=
    the params?

    This is what I've got on the go now:
    Dim strpage
    strpage = "a bunch of html..."
    Dim sec
    sec = Split(sections, ",")
    For i = LBound(sec) To UBound(sec)
    Set re1 = New RegExp
    re1.IgnoreCase = True
    re1.Global = False
    re1.Pattern = "<!--tag:" & sec(i) & ">(.*)?<!--/tag:" & sec(i) & ">"
    Set Matches = re1.Execute(strpage)

    strpage = Replace(strpage, Matches.Item(0).SubMatches(0), "")

    Next
     
    j1c, Mar 15, 2005
    #5
  6. j1c

    Guest

    j1c wrote:
    > How can I remove the content in between tags? I have a page that has
    > several custom tags: <!--tag:1--> Content 1 <!--/tag:1--> <br>
    > <!--tag:2--> Content 2 <!--/tag:2--> <br> <!--tag:3--> Content 3
    > <!--/tag:3--> If I only wanted to see the contents of tag 2 for
    > example, how could I strip out 1 and 3?



    Load it into frontpage 2003. It has more advanced editing features
    such as this.
     
    , Mar 15, 2005
    #6
  7. "j1c" <> wrote in message
    news:...
    > How can I remove the content in between tags? I have a page that has
    > several custom tags: <!--tag:1--> Content 1 <!--/tag:1--> <br>
    > <!--tag:2--> Content 2 <!--/tag:2--> <br> <!--tag:3--> Content 3
    > <!--/tag:3--> If I only wanted to see the contents of tag 2 for
    > example, how could I strip out 1 and 3?
    >


    <%
    Dim s, arr, re
    s = _
    "<!--tag:1-->Content 1<!--/tag:1--><br>" &_
    "<!--tag:2-->Content 2<!--/tag:2--><br>" &_
    "<!--tag:3-->Content 3<!--/tag:3--><br>" &_
    "<!--tag:4-->Content 4<!--/tag:4--><br>" &_
    "<!--tag:5-->Content 5<!--/tag:5--><br>" &_
    "<!--tag:6-->Content 6<!--/tag:6--><br>" &_
    "<!--tag:7-->Content 7<!--/tag:7--><br>" &_
    "<!--tag:8-->Content 8<!--/tag:8--><br>" &_
    "<!--tag:9-->Content 9<!--/tag:9--><br>" &_
    "<!--tag:10-->Content 10<!--/tag:10-->"

    arr = Array(1,5,7)

    Set re = New RegExp
    re.Global = True
    re.IgnoreCase = True
    re.Pattern = "(<!--tag:(?!(" & Join(arr,"|") &
    ")-)(\d+)-->).*(<!--/tag:\3-->)"

    Response.Write re.Replace(s,"$1$4")
    %>
     
    Chris Hohmann, Mar 15, 2005
    #7
  8. "Chris Hohmann" <> wrote in message
    news:...
    > "j1c" <> wrote in message
    > news:...
    >> How can I remove the content in between tags? I have a page that has
    >> several custom tags: <!--tag:1--> Content 1 <!--/tag:1--> <br>
    >> <!--tag:2--> Content 2 <!--/tag:2--> <br> <!--tag:3--> Content 3
    >> <!--/tag:3--> If I only wanted to see the contents of tag 2 for
    >> example, how could I strip out 1 and 3?
    >>

    >
    > <%
    > Dim s, arr, re
    > s = _
    > "<!--tag:1-->Content 1<!--/tag:1--><br>" &_
    > "<!--tag:2-->Content 2<!--/tag:2--><br>" &_
    > "<!--tag:3-->Content 3<!--/tag:3--><br>" &_
    > "<!--tag:4-->Content 4<!--/tag:4--><br>" &_
    > "<!--tag:5-->Content 5<!--/tag:5--><br>" &_
    > "<!--tag:6-->Content 6<!--/tag:6--><br>" &_
    > "<!--tag:7-->Content 7<!--/tag:7--><br>" &_
    > "<!--tag:8-->Content 8<!--/tag:8--><br>" &_
    > "<!--tag:9-->Content 9<!--/tag:9--><br>" &_
    > "<!--tag:10-->Content 10<!--/tag:10-->"
    >
    > arr = Array(1,5,7)
    >
    > Set re = New RegExp
    > re.Global = True
    > re.IgnoreCase = True
    > re.Pattern = "(<!--tag:(?!(" & Join(arr,"|") &
    > ")-)(\d+)-->).*(<!--/tag:\3-->)"
    >
    > Response.Write re.Replace(s,"$1$4")
    > %>
    >


    You may want to throw in a non-greedy clause if it's possible that tags
    would repeat. The new pattern would be

    re.Pattern = "(<!--tag:(?!(" & Join(arr,"|") &
    ")-)(\d+)-->).*?(<!--/tag:\3-->)"

    HTH
    -Chris Hohmann
     
    Chris Hohmann, Mar 15, 2005
    #8
  9. j1c

    j1c Guest

    Thanks for the tips - it is working very well.

    1 thing though - in the RegEx what would I need to change to allow
    either:

    <!--tag:01-->content<!--/tag:01-->

    OR

    <!--tag:01-->
    content
    <!--/tag:01-->
     
    j1c, Mar 16, 2005
    #9
  10. "j1c" <> wrote in message
    news:...
    > Thanks for the tips - it is working very well.
    >
    > 1 thing though - in the RegEx what would I need to change to allow
    > either:
    >
    > <!--tag:01-->content<!--/tag:01-->
    >
    > OR
    >
    > <!--tag:01-->
    > content
    > <!--/tag:01-->
    >


    <%
    Dim s, arr, re
    s = _
    "<!--tag:1-->Content 1<!--/tag:1--><br>" &_
    "<!--tag:2-->Content 2<!--/tag:2--><br>" &_
    "<!--tag:3-->Content 3<!--/tag:3--><br>" &_
    "<!--tag:4-->Content 4<!--/tag:4--><br>" &_
    "<!--tag:5-->Content 5<!--/tag:5--><br>" &_
    "<!--tag:6-->Content 6<!--/tag:6--><br>" &_
    "<!--tag:7-->Content 7<!--/tag:7--><br>" &_
    "<!--tag:8-->Content 8<!--/tag:8--><br>" &_
    "<!--tag:9-->Content 9<!--/tag:9--><br>" &_
    "<!--tag:10-->" & vbCRLF & "Content 10" & vbCRLF & "<!--/tag:10-->"

    arr = Array(1,5,7)

    Set re = New RegExp
    re.Global = True
    re.IgnoreCase = True
    re.MultiLine=True
    re.Pattern = "(<!--tag:(?!(" & Join(arr,"|") &
    ")-)(\d+)-->)(.|\n)*?(<!--/tag:\3-->)"

    Response.Write re.Replace(s,"$1$5")
    %>
     
    Chris Hohmann, Mar 16, 2005
    #10
  11. j1c

    j1c Guest

    thanks for the tips guys
     
    j1c, Mar 21, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Ebdrup
    Replies:
    0
    Views:
    2,723
    Allan Ebdrup
    Jan 26, 2006
  2. Jarry
    Replies:
    2
    Views:
    517
    Andy Dingley
    Aug 29, 2006
  3. mcnewsxp
    Replies:
    6
    Views:
    1,370
    dorayme
    Nov 25, 2010
  4. Aaron  Fleming

    Remove content between tags

    Aaron Fleming, Mar 15, 2005, in forum: ASP General
    Replies:
    1
    Views:
    124
    McKirahan
    Mar 15, 2005
  5. replacing tags between tags

    , Sep 18, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    136
    J├╝rgen Exner
    Sep 19, 2005
Loading...

Share This Page