简体   繁体   English

RegEx-Stylish-排除某些页面

[英]RegEx-Stylish - Excluding some pages

I'm trying to use regex on Stylish addon for browsers, to match a website. 我正在尝试在用于浏览器的时尚插件上使用正则表达式来匹配网站。

This regex needs to match one domain (we will name it website.com ) 此正则表达式需要匹配一个域(我们将其命名为website.com

And this is how it should work : 这就是它应该如何工作的:

  • any subdomain allowed 允许的任何子域
  • http or https too http或https
  • website.com/team* => not allowed website.com/team* =>不允许
  • website.com/forum* => not allowed website.com/forum* =>不允许
  • website.com* => allowed website.com * =>允许

This litteraly mean it should work for any pages of the website but any links with /team or /forum right after the .com don't work. 从某种意义上说,它应适用于网站的任何页面,但.com后立即与/ team或/ forum的任何链接均无效。

I tried that 我尝试过

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

But it doesn't work and I don't know how to make it to match only the domain website.com 但这不起作用,我也不知道如何使其仅与域名website.com相匹配

Just another question, is it this kind of regular expression that work for Stylish? 另一个问题是,这种正则表达式对时尚用户有用吗? I didn't find anything on Google about it 我在Google上找不到任何相关信息

The \\w{2,6} does not match website because that contains 7 characters. \\w{2,6}website不匹配,因为它包含7个字符。 The character class at the end ([^\\s]+ which will match not a whitespace character could use a quantifier of 0+ times using an * to also match when there is no trailing forward slash. 末尾的字符类([^\\s]+将不与空格字符匹配,可以使用0+次的量词,并使用*进行匹配,当没有尾随的正斜杠时也可以匹配。

If you want to match website.com, you could also match the .com part or else the [^\\s]* will match the rest of the url. 如果要匹配website.com,也可以匹配.com部分,否则[^\\s]*将匹配其余的URL。

The forward slash should be part of the negative lookahead as that is the string you don't want to be directly on the right. 正斜杠应为负前瞻的一部分,因为这是您不想直接位于右侧的字符串。

Your pattern might look like: 您的模式可能如下所示:

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

That will match 那会匹配

  • \\b(?:https?:\\/\\/)? Word boundary followed by optional http(s):// 单词边界,后跟可选的http(s)://
  • (?:\\w+\\.)* Match 0+ times 1+ word chars followed by a dot (?:\\w+\\.)*匹配0+次1+个单词字符,后跟一个点
  • website\\.com Match website.com website\\.com匹配website.com
  • (?!\\/team|\\/forum) Negative lookahead to assert what is directly on the right is not /team or /forum (?!\\/team|\\/forum)负向断言来断言直接在右边的不是/ team或/ forum
  • \\S* Match 0+ times a non whitespace character \\S*匹配0+次非空格字符

Regex demo 正则表达式演示

Check the following regex, 检查以下正则表达式,

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

Click here for demo. 单击此处进行演示。 here you can find every part of the regex has been broken down for your understanding 在这里您可以找到正则表达式的每个部分,以供您理解

This regex is tested on the following test cases 此正则表达式在以下测试用例上进行了测试

  1. www.website.com = allowed www.website.com =允许
  2. https://www.website.com = allowed https://www.website.com =允许
  3. http://www.website.com = allowed https://www.website.com/team = not allowed http://www.website.com =允许https://www.website.com/team =不允许
  4. https://www.website.com/forum = not allowed https://www.website.com/forum =不允许
  5. https://www.website.com/samplepage = allowed 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/')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM