-1

有一个block div,在他里面有一个未知数量的链接,如“a href onclick”,如果有多个链接,则用逗号和空格分隔。

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**
}

}

我认为应该使用它([^ <] *)来代替(. +?),但它根本不起作用。

4

1 回答 1

0

如果使用正则表达式是理想的(它们不是),我会使用两个单独的表达式,一个查找 <div class="labeled fl_l"> 和 </div> 之间的所有内容,然后另一个查找每个关联。

但是,正则表达式不是适合这项工作的工具。您可能想要考虑使用 xPath 来遍历链接。

于 2013-09-30T20:48:13.540 回答