如何查找所有出现的字符串?

时间:2019-11-19 03:19:39

标签: javascript tampermonkey

嗨,我在尝试使用代码时遇到问题,但我失败了,我想在内部html中查找所有文本以进行计算,但是我的代码却得到的第一行不是html中的全部 这是我的代码

HTML

<a href="http://wwwgoogg.com/asas/1.mkv">download</a>
<a href="http://wwwgoogg.com/asas/12.mkv">download</a>
<a href="http://wwwgoogg.com/asas/13.mkv">download</a>
<a href="http://wwwgoogg.com/asas/16.mkv">download</a>
<a href="http://wwwgoogg.com/asas/22.mkv">download</a>

脚本

// ==UserScript==
// @name         New Userscript
// @match        file:///E:/Physics.html
// ==/UserScript==
/* global $ */
(function() {
    var full1 = document.body.innerHTML
  var first1 = full1.indexOf("wwwgoogg");
  var first2 = full1.slice(first1);
  var last1 = first2.indexOf("mkv");
var aa1 = full1.substring((last1-2+first1),(first1));
    alert (aa1);

})();

wwwgoogg.com/asas/1.mkv
wwwgoogg.com/asas/12.mkv
wwwgoogg.com/asas/221.mkv
wwwgoogg.com/asas/133.mkv

提前感谢

3 个答案:

答案 0 :(得分:1)

我已经定义了一个正则表达式并将其全局应用。 要获取答案,请参见控制台

var testStr = "<a href='http://wwwgoogg.com/asas/1.mkv'>download</a> <a href='http://wwwgoogg.com/asas/12.mkv'>download</a> <a href='http://wwwgoogg.com/asas/13.mkv'>download</a> <a href='http://wwwgoogg.com/asas/16.mkv'>download</a> <a href='http://wwwgoogg.com/asas/22.mkv'>download</a>"
var regex = /wwwgoogg/gi;
var result;
var indices = [];
while ((result = regex.exec(testStr))) {
  indices.push(result.index);
}
console.log(indices);

答案 1 :(得分:0)

您可以将HTML文本与RegExp匹配,并获得所有结果。请参见以下示例:

const text = `
<a href="http://wwwgoogg.com/asas/1.mkv">download</a>
<a href="http://wwwgoogg.com/asas/12.mkv">download</a>
<a href="http://wwwgoogg.com/asas/13.mkv">download</a>
<a href="http://wwwgoogg.com/asas/16.mkv">download</a>
<a href="http://wwwgoogg.com/asas/22.mkv">download</a>
`;

const matches = text.match(/https?:\/\/wwwgoogg.com\/[\w/.]+/gm);
console.log(matches);

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<body>
<a class="input" href="http://wwwgoogg.com/asas/1.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/12.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/13.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/16.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
<a class="input" href="http://wwwgoogg.com/asas/22.mkv">download</a>
</body>
</html>

和代码

// ==UserScript==
// @name         New Userscript
// @match        file:///E:/Physics.html
// @grant        GM_openInTab
// ==/UserScript==
/* global $ */
(function() {
var x = document.getElementsByClassName("input").length;
    var i ;
    for (i = 0; i < x; i++) {
  var fields = document.getElementsByClassName("input")[i].getAttribute('href');
GM_openInTab(fields);
  }



    //var bb = document.getElementsByClassName("input")[i].getAttribute('href');
  // GM_openInTab (bb);
})(); 

这就是我想要的 非常感谢您的帮助,我真的很感激,并且我学到了很多东西,而且我正在学习