RegEx-Stylish - 排除某些页面

RegEx-Stylish - Excluding some pages

我正在尝试在浏览器的 Stylish 插件上使用正则表达式来匹配网站。

此正则表达式需要匹配一个域(我们将其命名为website.com

它应该是这样工作的:

从字面上看,它应该适用于网站的任何页面,但在 .com 之后与 /team 或 /forum 的任何链接都不起作用。

我试过了

((\w+)*\.\w{2,6})(\/)(?!team|forum)([^\s]+)

但是它不起作用,我不知道如何让它只匹配域 website.com

另外一个问题,这种正则表达式是否适用于 Stylish?我在 Google 上没有找到任何关于它的信息

\w{2,6} 不匹配 website 因为它包含 7 个字符。 ([^\s]+ 末尾的字符 class 将不匹配空白字符,可以使用 0+ 次量词使用 * 在没有尾部正斜杠时也匹配。

如果你想匹配 website.com,你也可以匹配 .com 部分,否则 [^\s]* 将匹配 url 的其余部分。

正斜杠应该是负前瞻的一部分,因为这是您不希望直接位于右侧的字符串。

您的模式可能如下所示:

\b(?:https?:\/\/)?(?:\w+\.)*website\.com(?!\/team|\/forum)\S*

那将匹配

  • \b(?:https?:\/\/)? 单词边界后跟可选的 http(s)://
  • (?:\w+\.)* 匹配 0+ 次 1+ 个单词字符后跟一个点
  • website\.com 匹配 website.com
  • (?!\/team|\/forum) 断言直接在右边的内容不是 /team 或 /forum
  • 的否定前瞻
  • \S* 匹配 0+ 次非空白字符

Regex demo

检查以下正则表达式,

(https?:\/\/)?(www.website.com)(\/)?(?!team|forum)(\w)*

点击here进行演示。在这里你可以找到正则表达式的每一部分都被分解以供你理解

此正则表达式在以下测试用例上进行了测试

  1. www.website.com = 允许
  2. https://www.website.com = 允许
  3. http://www.website.com = allowed https://www.website.com/team = 不允许
  4. https://www.website.com/forum = 不允许
  5. https://www.website.com/samplepage = 允许

function Test_1(path){
return /^(https|http)(:\/\/)(www\.|)((?!website)[\w]*?\.|)website\.com((\/)|(\/)((?!forum\/|team\/).*?)|)$/gi.test(path);
}

console.log(Test_1('http://website.com'));
console.log(Test_1('https://www.website.com'));
console.log(Test_1('http://websit.website.com'));
console.log(Test_1('http://websit.website.com/'));
console.log(Test_1('http://websit.website.com/seeg/yukyuk'));

console.log('--------------------------');
console.log('---[Other domain]--------');
console.log('--------------------------');

console.log(Test_1('http://website5.com'));
console.log(Test_1('https://www.website5.com'));
console.log(Test_1('http://websit.website5.com'));
console.log(Test_1('http://websit.website5.com/'));
console.log(Test_1('http://websit.website5.com/seeg/yukyuk'));

console.log('--------------------------');
console.log('---[forum domain]--------');
console.log('--------------------------');

console.log(Test_1('http://website.com/forum'));
console.log(Test_1('http://website.com/forum/'));
console.log(Test_1('http://website.com/forum/rgrg/'));
console.log(Test_1('http://website.com/3forum/rgrg/'));
console.log(Test_1('http://website.com/forum5/rgrg/'));

console.log('--------------------------');
console.log('---[forum subdomain]------');
console.log('--------------------------');

console.log(Test_1('http://websit.website.com/forum'));
console.log(Test_1('http://websit.website.com/forum/'));
console.log(Test_1('http://websit.website.com/forum/rgrg/'));
console.log(Test_1('http://websit.website.com/3forum/rgrg/'));
console.log(Test_1('http://websit.website.com/forum5/rgrg/'));


console.log('--------------------------');
console.log('---[team domain]---------');
console.log('--------------------------');

console.log(Test_1('http://websit.website.com/team'));
console.log(Test_1('http://websit.website.com/team/'));
console.log(Test_1('http://websit.website.com/team/rgrg/'));
console.log(Test_1('http://websit.website.com/3team/rgrg/'));
console.log(Test_1('http://websit.website.com/team5/rgrg/'));


console.log('--------------------------');
console.log('---[team subdomain]-------');
console.log('--------------------------');


console.log(Test_1('http://websit.website.com/team'));
console.log(Test_1('http://websit.website.com/team/'));
console.log(Test_1('http://websit.website.com/team/rgrg/'));
console.log(Test_1('http://websit.website.com/3team/rgrg/'));
console.log(Test_1('http://websit.website.com/team5/rgrg/'));