匹配URL的Regex不起作用

Regex to match URL not working

本文关键字:不起作用 Regex URL 匹配      更新时间:2023-09-26

我有以下函数,该函数循环浏览浏览器历史记录(使用chrome API)并提取任何google搜索查询。我在各种网站上测试了regex,它可以工作,但在我创建的JS函数中似乎不起作用(不记录任何URL):

function getHistory(){
          var regex = '^https?:'/'/[^/]+'.google'.[a-z.]+'/((search[?#])|(webhp[?#])|([?#])).*q=';
          chrome.history.search({text:regex, maxResults:11}, function(data) {
              data.forEach(function(page) {
                  var url = page.url;
                  console.log(url);
          });
       });
    }

根据单据它看起来不像chrome.history.search()采用正则表达式,而是采用文本搜索查询,因此它找不到任何看起来像字面正则表达式的URL也就不足为奇了。

虽然效率可能不高,但您可以将文本留空或使用"google.com",然后对结果执行regex过滤。