-1

There is a block div, in him an unknown number of links like "a href onclick", if there are link more one, then they are separated a comma and a space.

var reg = /<div class="labeled fl_l"><a href="[^"]*" onclick="[^"]*">(.+?)<\/a>(, <a href="[^"]*" onclick="[^"]*">(.+?)<\/a>{1,})?<\/div>/mg;
var arr;
while ((arr = reg.exec(data)) != null) {
            console.log(arr[0]); //contains the entire text (because it is java script)
    console.log(arr[1]); //contains the name of the first link
    console.log(arr[2]); //contains the following "a href" entirely (if I will point out (?: x, <a... /a>), then the nested brackets will not work)
    console.log(arr[3]); //contains the name of the second link, **and then all of the code**
}

}

I think that should be used ([^ <] *) instead of (. +?), but it does not work at all then.

CSᵠ
  • 10,049
  • 9
  • 41
  • 64
  • 7
    I'll stop you right away. It's time for you to know the thruth... http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – plalx Sep 30 '13 at 20:19
  • Aside from plalx's comment, if you are going to ask a question about a regex, at least give some examples of the source and what you expect to extract. Then forget regex and use the DOM manipulation functions in Javascript that are designed for the purpose. – Matt Burland Sep 30 '13 at 20:20
  • Don't parse HTML with a regular expression. You're in JavaScript, don't you have access to the DOM elements themselves? It's already parsed, you just need to access it. – tadman Sep 30 '13 at 20:24
  • Start with `getElementByClass` to get the parent div and then `getElementByTagName` to get all the `A` tags. Then you can use `getAttributeNode` to get whatever attributes you are interested in. – Matt Burland Sep 30 '13 at 20:27

1 Answers1

0

If using regular expressions were ideal for this (which they aren't), I'd go with two separate expressions, one to find everything between <div class="labeled fl_l"> and </div> and then another to find each link.

However, regular expressions aren't the right tool for the job. You might want to instead consider using xPath to iterate through the links.

Community
  • 1
  • 1
dgmdan
  • 405
  • 6
  • 14