RegExp匹配所有

时间:2017-02-23 02:04:43

标签: javascript regex

由于某种原因,我似乎无法让RegExp匹配所有事件。现在已经很晚了,我确定我做错了什么,所以任何帮助都会很棒!

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

1 个答案:

答案 0 :(得分:1)

最好的方法是单独迭代public view onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.reference, null, false); ViewPager viewPager = (ViewPager) view.findViewById(R.id.reference); viewPager.setAdapter(new ReferenceTabAdapter()); viewPager.setCurrentItem(1); return view; } 个元素。然后,您可以使用.match() method来解析li属性(而不是直接解析HTML)。

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

  if (match) {
    console.log(match);
  }
});
&#13;
&#13;
&#13;

但是,如果您决定不对元素进行迭代,那么您可以使用.match() method代替<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>,就像您原来那样。但是,由于您正在使用全局匹配,因此您无法获得所有捕获组。

&#13;
&#13;
.exec()
&#13;
var reg = />(Mozilla[^<]+)/gm;
console.log(document.body.innerHTML.match(reg));
&#13;
&#13;
&#13;

如果您想要所有捕获组,请使用.exec() method并增加<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>属性以获得下一个匹配项。在下面的代码段中,将记录第一个捕获组。

lastIndex

段:

&#13;
&#13;
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]);
}
&#13;
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]);
}
&#13;
&#13;
&#13;