SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    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.

  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)
    match() works differently when you use the global modifier.

    Try this.
    Code:
    var pattern=/<a id="linkSubAlbum[^>]+>(\w+)<\/a>/g;
    var results = [];
    while((temp = pattern.exec(str)) != null) {
      results.push(temp);
    }
    You might want this for versions of IE before 5.5:
    Code:
    if(![].push){ // for old browsers that don't natively support Array.push()
      Array.prototype.push=function(){
        for(var i=0;i<arguments.length;i++) this[this.length]=arguments[i];
        return this.length;
      }
    }
    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
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So I feared.

    Thanks Kravvitz, that works beautifully.

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome.

    If you didn't notice, I improved the regexp itself a bit too.

    I just thought of this: Are you sure that the ID will always be the first attribute?
    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.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes I did notice. Thanks.

    Yes, the ID is always the first attribute, at least every time for the 20-odd times I've tested it (I'm getting an entire page via an XHR request). Unfortunately it's HTML4 tag soup that I get, so I can't use DOMParser (this is for a Greasemonkey script, so I only need to support Mozilla).


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
  •