1

For some reason I can't seem to get RegExp to match all occurrences. It's late, I'm sure I'm doing something incorrect, so any help would be great!

HTML

<li><a href='/index.php?id=1769'>Opera/6.05 (Windows 2000; U)  [en]</a></li>
<li><a href='/index.php?id=3812'>Opera/6.05 (Windows 2000; U)  [de]</a></li>
<li><a href='/index.php?id=4962'>Mozilla/5.0 (Windows XP; U) Opera 6.05  [de]</a></li>
<li><a href='/index.php?id=3695'>Mozilla/5.0 (Windows NT 4.0; U) Opera 6.05  [en]</a></li>

JS

const src = document.body.innerHTML;
const reg = />(Mozilla[^<]+)/gm;
const res = reg.exec(src);

Live fiddle

4
  • I've just tested it and it's working! Feb 23, 2017 at 2:08
  • It's only showing the first match for myself, I want a list of every match.
    – daryl
    Feb 23, 2017 at 2:08
  • 1
    Use const res = src.match(reg);! Feb 23, 2017 at 2:11
  • @ibrahimmahrir - That doesn't return the first capturing group for each match though. Feb 23, 2017 at 2:33

1 Answer 1

1

The best approach would be to iterate over the li elements individually. Then you can use the .match() method to parse the textContent property (as opposed to parsing HTML directly).

var elements = document.querySelectorAll('li');
Array.from(elements).forEach(function (element) {
  var match = element.textContent.match(/^(Mozilla.*)/g);

  if (match) {
    console.log(match);
  }
});
<li><a href='/index.php?id=1769'>Opera/6.05 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=3812'>Opera/6.05 (Windows 2000; U)  [de]</a></li><li><a href='/index.php?id=4962'>Mozilla/5.0 (Windows XP; U) Opera 6.05  [de]</a></li><li><a href='/index.php?id=3695'>Mozilla/5.0 (Windows NT 4.0; U) Opera 6.05  [en]</a></li><li><a href='/index.php?id=2875'>Mozilla/5.0 (Windows ME; U) Opera 6.05  [de]</a></li><br><br><a href='/pages/useragentstring.php?all=TRUE&name=Opera&vers=6.05'>More Opera 6.05 user agents strings --&gt;&gt;</a><br /></ul><h4>Opera 6.04</h4><ul><li><a href='/index.php?id=2193'>Opera/6.04 (Windows XP; U)  [en]</a></li><li><a href='/index.php?id=3775'>Opera/6.04 (Windows XP; U)  [de]</a></li><li><a href='/index.php?id=3961'>Opera/6.04 (Windows NT 4.0; U)  [en]</a></li><li><a href='/index.php?id=2888'>Opera/6.04 (Windows NT 4.0; U)  [de]</a></li><li><a href='/index.php?id=2211'>Opera/6.04 (Windows 98; U)  [en-GB]</a></li><li><a href='/index.php?id=3570'>Opera/6.04 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=1767'>Opera/6.04 (Windows 2000; U)  [de]</a></li><li><a href='/index.php?id=2929'>Mozilla/5.0 (Windows 2000; U) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3360'>Mozilla/4.78 (Windows 2000; U) Opera 6.04  [de]</a></li><li><a href='/index.php?id=4025'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [fr]</a></li><li><a href='/index.php?id=4254'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3581'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [de]</a></li><li><a href='/index.php?id=3220'>Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0) Opera 6.04  [en]</a></li><li><a href='/index.php?id=4104'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04  [pl]</a></li><li><a href='/index.php?id=3508'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3664'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.04  [en]</a></li><li><a href='/index.php?id=4495'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.04  [de]</a></li><li><a href='/index.php?id=1415'> Opera/6.04 (Windows XP; U)  [de]</a></li><li><a href='/index.php?id=1338'> Opera/6.04 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=1563'> Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [en]</a></li><br><br><a href='/pages/useragentstring.php?all=TRUE&name=Opera&vers=6.04'>More Opera 6.04 user agents strings --&gt;&gt;</a><br /></ul><h4>Opera 6.03</h4><ul><li><a href='/index.php?id=4949'>Opera/6.03 (Windows NT 4.0; U)  [en]</a></li><li><a href='/index.php?id=5776'>Opera/6.03 (Windows 98; U) [en]</a></li><li><a href='/index.php?id=2883'>Opera/6.03 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=4375'>Opera/6.03 (Linux 2.4.18-18.7.x i686; U)  [en]</a></li><li><a href='/index.php?id=1872'>Mozilla/5.0 (Windows 2000; U) Opera 6.03  [en]</a></li><li><a href='/index.php?id=4955'>Mozilla/5.0 (Linux 2.4.18-18.7.x i686; U) Opera 6.03  [en]</a></li><li><a href='/index.php?id=2195'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.03  [en]</a></li><li><a href='/index.php?id=1873'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.03  [en]</a></li><li><a href='/index.php?id=3848'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.20-4GB i686) Opera 6.03  [en]</a></li><li><a href='/index.php?id=4951'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.19-4GB i686) Opera 6.03  [en]</a></li><li><a href='/index.php?id=2223'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.18-4GB i686) Opera 6.03  [en]</a></li>


However, if you decide not to iterate over the elements, then you can just use the .match() method in place of .exec() like you originally were. However, you wouldn't be able to get all the capturing groups since you're using a global match.

var reg = />(Mozilla[^<]+)/gm;
console.log(document.body.innerHTML.match(reg));
<li><a href='/index.php?id=1769'>Opera/6.05 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=3812'>Opera/6.05 (Windows 2000; U)  [de]</a></li><li><a href='/index.php?id=4962'>Mozilla/5.0 (Windows XP; U) Opera 6.05  [de]</a></li><li><a href='/index.php?id=3695'>Mozilla/5.0 (Windows NT 4.0; U) Opera 6.05  [en]</a></li><li><a href='/index.php?id=2875'>Mozilla/5.0 (Windows ME; U) Opera 6.05  [de]</a></li><br><br><a href='/pages/useragentstring.php?all=TRUE&name=Opera&vers=6.05'>More Opera 6.05 user agents strings --&gt;&gt;</a><br /></ul><h4>Opera 6.04</h4><ul><li><a href='/index.php?id=2193'>Opera/6.04 (Windows XP; U)  [en]</a></li><li><a href='/index.php?id=3775'>Opera/6.04 (Windows XP; U)  [de]</a></li><li><a href='/index.php?id=3961'>Opera/6.04 (Windows NT 4.0; U)  [en]</a></li><li><a href='/index.php?id=2888'>Opera/6.04 (Windows NT 4.0; U)  [de]</a></li><li><a href='/index.php?id=2211'>Opera/6.04 (Windows 98; U)  [en-GB]</a></li><li><a href='/index.php?id=3570'>Opera/6.04 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=1767'>Opera/6.04 (Windows 2000; U)  [de]</a></li><li><a href='/index.php?id=2929'>Mozilla/5.0 (Windows 2000; U) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3360'>Mozilla/4.78 (Windows 2000; U) Opera 6.04  [de]</a></li><li><a href='/index.php?id=4025'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [fr]</a></li><li><a href='/index.php?id=4254'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3581'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [de]</a></li><li><a href='/index.php?id=3220'>Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0) Opera 6.04  [en]</a></li><li><a href='/index.php?id=4104'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04  [pl]</a></li><li><a href='/index.php?id=3508'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3664'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.04  [en]</a></li><li><a href='/index.php?id=4495'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.04  [de]</a></li><li><a href='/index.php?id=1415'> Opera/6.04 (Windows XP; U)  [de]</a></li><li><a href='/index.php?id=1338'> Opera/6.04 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=1563'> Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [en]</a></li><br><br><a href='/pages/useragentstring.php?all=TRUE&name=Opera&vers=6.04'>More Opera 6.04 user agents strings --&gt;&gt;</a><br /></ul><h4>Opera 6.03</h4><ul><li><a href='/index.php?id=4949'>Opera/6.03 (Windows NT 4.0; U)  [en]</a></li><li><a href='/index.php?id=5776'>Opera/6.03 (Windows 98; U) [en]</a></li><li><a href='/index.php?id=2883'>Opera/6.03 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=4375'>Opera/6.03 (Linux 2.4.18-18.7.x i686; U)  [en]</a></li><li><a href='/index.php?id=1872'>Mozilla/5.0 (Windows 2000; U) Opera 6.03  [en]</a></li><li><a href='/index.php?id=4955'>Mozilla/5.0 (Linux 2.4.18-18.7.x i686; U) Opera 6.03  [en]</a></li><li><a href='/index.php?id=2195'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.03  [en]</a></li><li><a href='/index.php?id=1873'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.03  [en]</a></li><li><a href='/index.php?id=3848'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.20-4GB i686) Opera 6.03  [en]</a></li><li><a href='/index.php?id=4951'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.19-4GB i686) Opera 6.03  [en]</a></li><li><a href='/index.php?id=2223'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.18-4GB i686) Opera 6.03  [en]</a></li>


If you want all the capturing groups, use the .exec() method and increment the lastIndex property to get the next match. In the snippet below, the first capturing group is logged.

var reg = />(Mozilla[^<]+)/gm;
var match;

while ((match = reg.exec(document.body.innerHTML)) !== null) {
    if (match.index === reg.lastIndex) {
        reg.lastIndex++;
    }

    console.log(match[1]);
}

Snippet:

var reg = />(Mozilla[^<]+)/gm;
var match;

while ((match = reg.exec(document.body.innerHTML)) !== null) {
    if (match.index === reg.lastIndex) {
        reg.lastIndex++;
    }
    
    console.log(match[1]);
}
<li><a href='/index.php?id=1769'>Opera/6.05 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=3812'>Opera/6.05 (Windows 2000; U)  [de]</a></li><li><a href='/index.php?id=4962'>Mozilla/5.0 (Windows XP; U) Opera 6.05  [de]</a></li><li><a href='/index.php?id=3695'>Mozilla/5.0 (Windows NT 4.0; U) Opera 6.05  [en]</a></li><li><a href='/index.php?id=2875'>Mozilla/5.0 (Windows ME; U) Opera 6.05  [de]</a></li><br><br><a href='/pages/useragentstring.php?all=TRUE&name=Opera&vers=6.05'>More Opera 6.05 user agents strings --&gt;&gt;</a><br /></ul><h4>Opera 6.04</h4><ul><li><a href='/index.php?id=2193'>Opera/6.04 (Windows XP; U)  [en]</a></li><li><a href='/index.php?id=3775'>Opera/6.04 (Windows XP; U)  [de]</a></li><li><a href='/index.php?id=3961'>Opera/6.04 (Windows NT 4.0; U)  [en]</a></li><li><a href='/index.php?id=2888'>Opera/6.04 (Windows NT 4.0; U)  [de]</a></li><li><a href='/index.php?id=2211'>Opera/6.04 (Windows 98; U)  [en-GB]</a></li><li><a href='/index.php?id=3570'>Opera/6.04 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=1767'>Opera/6.04 (Windows 2000; U)  [de]</a></li><li><a href='/index.php?id=2929'>Mozilla/5.0 (Windows 2000; U) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3360'>Mozilla/4.78 (Windows 2000; U) Opera 6.04  [de]</a></li><li><a href='/index.php?id=4025'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [fr]</a></li><li><a href='/index.php?id=4254'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3581'>Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [de]</a></li><li><a href='/index.php?id=3220'>Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0) Opera 6.04  [en]</a></li><li><a href='/index.php?id=4104'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04  [pl]</a></li><li><a href='/index.php?id=3508'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04  [en]</a></li><li><a href='/index.php?id=3664'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.04  [en]</a></li><li><a href='/index.php?id=4495'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.04  [de]</a></li><li><a href='/index.php?id=1415'> Opera/6.04 (Windows XP; U)  [de]</a></li><li><a href='/index.php?id=1338'> Opera/6.04 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=1563'> Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.04  [en]</a></li><br><br><a href='/pages/useragentstring.php?all=TRUE&name=Opera&vers=6.04'>More Opera 6.04 user agents strings --&gt;&gt;</a><br /></ul><h4>Opera 6.03</h4><ul><li><a href='/index.php?id=4949'>Opera/6.03 (Windows NT 4.0; U)  [en]</a></li><li><a href='/index.php?id=5776'>Opera/6.03 (Windows 98; U) [en]</a></li><li><a href='/index.php?id=2883'>Opera/6.03 (Windows 2000; U)  [en]</a></li><li><a href='/index.php?id=4375'>Opera/6.03 (Linux 2.4.18-18.7.x i686; U)  [en]</a></li><li><a href='/index.php?id=1872'>Mozilla/5.0 (Windows 2000; U) Opera 6.03  [en]</a></li><li><a href='/index.php?id=4955'>Mozilla/5.0 (Linux 2.4.18-18.7.x i686; U) Opera 6.03  [en]</a></li><li><a href='/index.php?id=2195'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.03  [en]</a></li><li><a href='/index.php?id=1873'>Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.03  [en]</a></li><li><a href='/index.php?id=3848'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.20-4GB i686) Opera 6.03  [en]</a></li><li><a href='/index.php?id=4951'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.19-4GB i686) Opera 6.03  [en]</a></li><li><a href='/index.php?id=2223'>Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.18-4GB i686) Opera 6.03  [en]</a></li>

1
  • I didn't provide context, but I'm scraping from a server so looping over nodes is not the best idea. But yeah, String.prototype.match was what I was after. A total brain fart. Thanks for the in-depth answer!
    – daryl
    Feb 23, 2017 at 2:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.