2016-04-09 30 views

回答

1

您可以使用filter()

reg = /https:\/\/my_site.com\/XX-\d{4}$/g; 
elements = $(document) 
    .find("a") 
    .filter(function(){ 
     return reg.test(this.href); 
    }); 
return elements; 
0

數據來源在哪裏?

我懷疑你試圖讀取的數據是爲安全傳輸編碼的。例如,這是空間轉換爲%20的位置。

如果爲true,則需要使用encodeURIComponent()轉換源數據,然後應用您的查找。

這可能工作(雖然我的搜索使用較弱)。我沒有測試代碼,但應該給你方向的想法...

// Collate all href from the document and store in array links 
var links=[]; 
$(document).find("a").each(
function() 
{ 
links.push(encodeURIComponent($(this).prop("href"))); 
}); 

// Loop thru array links, perform your search on each element, 
// store result in array results 
var results=[]; 
results=links.filter(function(item){ 
    return item.search('/\d(?=\d{4})/g'); 
}); 

console.log(results); 

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

1

您可以使用filter()attribute starts with selector

var regex = /XX-\d{4}$/; // Exact four digits after XX- 

var anchors = $(document.body) 
    .find('a[href^="https://my_site.com/XX-"]') 
    .filter(() => regex.test(this.href)); 
0

根本不需要jQuery。完全可以通過純JS完成一行。它可能快多倍。

var as = document.getElementsByTagName("a"), 
    ael = Array.prototype.filter.call(as, e => /XX-\d{4}$/g.test(e.href));