【问题标题】:How to exlude word using Regex?如何使用正则表达式排除单词?
【发布时间】:2018-09-03 19:06:14
【问题描述】:

我尝试下面的代码,目标是使用自己的firefox插件阻止第三方的所有图像

所以,例如: 如果我打开 www.test.com 页面,我同意查看来自活动域 (www.test.com) 的所有图片,但我想全部屏蔽其他图片www.boring-ads.com、www.iamthebest.net、...

var MyPattern= /^((?!goodimg\.net).)*$/ig;

function cancel(requestDetails) {
   //console.log("Canceling: " + requestDetails.url);
   return {cancel: true};
}

browser.webRequest.onBeforeRequest.addListener(
   cancel,
   {urls: [MyPattern], types: ["image"]},
   ["blocking"]
);

所有来自 goodimg.net 的图片都已加载,但已经来自其他域 :(((

为什么? ...无法使用正则表达式排除字符串?

【问题讨论】:

  • 欢迎来到 StackOverflow。目前,您的问题还不是很清楚,我们很难帮到您。问题why regex no match exclusion? 的标题令人困惑,问题的其余部分也是如此。明确你想要达到的目标。

标签: javascript regex pattern-matching firefox-addon webrequest


【解决方案1】:

Firefox(或 chrome)不使用 RegEx 来检查 url, 请阅读Match_patterns 您可以发送您的模式匹配:

/^(\*|http|https|ws|wss|file|ftp):\/\/(\*|(?:\*\.)?(?:[^\/*]+))?\/(.*)$/

matchPatternToRegExp

你可以在方法cancel中使用<all_urls>和url

【讨论】:

    猜你喜欢
    • 2016-05-09
    • 2021-04-20
    • 2017-03-13
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多