簡體   English   中英

如何找到所有出現的字符串?

[英]How to find all occurrences of a string?

嗨,我在嘗試代碼時遇到問題,但我失敗了

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

提前致謝

我已經定義了一個正則表達式並在全球范圍內應用它。 答案請看控制台

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

<!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);
})(); 

這就是我想要的,非常感謝你幫助我,我真的很感激,我學到了很多,我正在學習

您可以將 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM