gpt4 book ai didi

javascript - 正则表达式匹配所有

转载 作者:太空宇宙 更新时间:2023-11-04 15:59:20 25 4
gpt4 key购买 nike

出于某种原因,我似乎无法让 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

最佳答案

最好的方法是单独迭代 li 元素。然后您可以使用.match() method解析 textContent 属性(而不是直接解析 HTML)。

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>

<小时/>

但是,如果您决定不迭代元素,那么您可以只使用 .match() method代替 .exec() 就像你原来那样。但是,由于您使用的是全局匹配,因此您将无法获取所有捕获组。

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>

<小时/>

如果您想要所有捕获组,请使用 .exec() method并增加 lastIndex 属性以获取下一个匹配项。在下面的代码片段中,记录了第一个捕获组。

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

片段:

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>

关于javascript - 正则表达式匹配所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com