Javascript regular expressions - exec infinite loop

后端 未结 1 757
醉酒成梦
醉酒成梦 2021-01-17 22:03

I\'m trying to get a link text using regex. there are possibly several links that may match the pattern and I want to get the furthest one until the 4th. Here is my JS code:

相关标签:
1条回答
  • 2021-01-17 22:23

    RegExp.exec, I believe, makes use of the lastIndex property and continually modifies it to make things like "global group capturing" possible; for it to work you need to have a single regular expression. Currently you're creating a new one on every iteration so it won't work...

    Try this:

    var level = 1;
    var pattern = /<a href="http:\/\/www.mysite.com\/x\/(?:.*)>(.*)<\/a>/img;
    var _match;
    while ( _match = pattern.exec(_html)){
         if (level < 5)  (_anchor_text=_match[1]);
         level ++;
    }
    
    0 讨论(0)
提交回复
热议问题