油猴脚本正则匹配出所有链接
时间: 2023-11-05 20:04:23 浏览: 155
以下是一个简单的油猴脚本,可以使用正则表达式匹配所有链接:
```javascript
// ==UserScript==
// @name Link Matcher
// @namespace http://tampermonkey/net/linkmatcher
// @version 1
// @description Matches all links on the page using a regular expression and logs them to the console
// @match http://*/*
// @match https://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Define the regular expression to match links
const linkRegex = /https?:\/\/[^\s]+/g;
// Find all links on the page
const links = document.getElementsByTagName('a');
// Loop through the links and log any matches to the console
for (let i = 0; i < links.length; i++) {
const href = links[i].href;
const matches = href.match(linkRegex);
if (matches) {
console.log(matches);
}
}
})();
```
该脚本将在页面加载时运行,查找所有链接并使用正则表达式匹配它们。如果有任何匹配项,它们将被记录到控制台中。您可以根据需要更改正则表达式来匹配不同类型的链接。