#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13

    Match html tag with dynamic attributes


    Hi,

    I have the following tag

    <a href="#" jQuery23482394798="3">Add</a>

    or it could just be

    <a href="#">Add</a>

    And i'm trying to match that whole thing even though the "jquery attribute" is dynamic.

    I kinda though I could try something like

    /<a href="#"\*.>Add</a>/i

    but that doesn't work for me...

    TIA
  2. #2
  3. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Try this:
    Code:
    /<a\s+href="#"[^>]*>Add<\/a>/i
    Note that although the slash is not a regex meta character, it needs escaping because you are using it as a delimiter for your pattern.
    If using Perl or PHP, you could use another delimiter:
    Code:
    @<a\s+href="#"[^>]*>Add</a>@i
    HTH
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Originally Posted by prometheuzz
    Try this:
    Code:
    /<a\s+href="#"[^>]*>Add<\/a>/i
    Note that although the slash is not a regex meta character, it needs escaping because you are using it as a delimiter for your pattern.
    If using Perl or PHP, you could use another delimiter:
    Code:
    @<a\s+href="#"[^>]*>Add</a>@i
    HTH
    Thanks I'll give it a go!
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Originally Posted by msteudel
    Thanks I'll give it a go!
    Hmmm that didn't seem to work for me.

    I don't think it makes a difference since I'm using the /i option, but the tag does have capital A's not sure why, so it looks like

    Code:
    <A href="#" jQuery1230918203="32">Add</A> - some other text
    I'm using javascript to do a search and replace of the A tag , but leaving everything after the tag (the dash and the text)

    My javascript looks like if it makes any difference:
    Code:
    value = value.replace( '/<a\s+href="#"[^>]*>Add<\/a>/i', '<a href="#" class="remove">Remove</a>' );
  8. #5
  9. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by msteudel
    ...

    My javascript looks like if it makes any difference:
    Code:
    value = value.replace( '/<a\s+href="#"[^>]*>Add<\/a>/i', '<a href="#" class="remove">Remove</a>' );
    Yes, that makes a lot of difference. In JavaScript, you don't need to "quote" your regex pattern. So instead of doing:

    Code:
    '/<a\s+href="#"[^>]*>Add<\/a>/i'
    you should do:

    Code:
    /<a\s+href="#"[^>]*>Add<\/a>/i
    This is a classic example why it is always a good idea to:
    1 - mention what tool/language you're using;
    2 - provide a SSCCE so that people answering questions can execute your code and will be able to see exactly what you see.

    Good luck!

    Comments on this post

    • ishnid agrees
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Sorry about that! Yeah that worked out great, the final code was something like:

    Code:
    value = value.replace( /<a\s+href="#"[^>]*>Add<\/a>/i, '<a href="#" class="remove">Remove</a>' );
    And that removed any dynamic attributes in the a tag after the href"#".

    Thanks again.
  12. #7
  13. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by msteudel
    Sorry about that! Yeah that worked out great, the final code was something like:

    Code:
    value = value.replace( /<a\s+href="#"[^>]*>Add<\/a>/i, '<a href="#" class="remove">Remove</a>' );
    And that removed any dynamic attributes in the a tag after the href"#".

    Thanks again.
    No problem and I'm glad you've got it sorted!

IMN logo majestic logo threadwatch logo seochat tools logo