SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Threaded View

  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    2 Post(s)
    0 Thread(s)

    Global regex matches

    There is the following in a string (can't use DOM methods or anything of the sort):
    HTML Code:
    <a id="linkSubAlbumXXX" class="something" title="somethingelse">Text</a>
    There are many of these and class and title vary, as does XXX (some numbers). I want to retrieve the Text for each one, so I tried the following regex:
    Code javascript:
    //str = very long string of HTML
    var s = str.match(/<a id="linkSubAlbum.+">(\w+)<\/a>/g);
    This matches every anchor that matches the pattern, which is great, but the returned array doesn't also contain the bit I have put in capturing parentheses (the Text I want to get). If I remove the g flag, I get the entire anchor and then also the Text, but only for the first match. I can understand why this happens, but not why when doing a global match, the capturing parentheses seem to be ignored.

    It seems like it's not possible to get what I want out of this with a single regular expression. Is this true? Thanks.


Posting Permissions

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