2

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)

And this is how it should work :

  • any subdomain allowed
  • http or https too
  • website.com/team* => not allowed
  • website.com/forum* => not allowed
  • website.com* => allowed

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.

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

Just another question, is it this kind of regular expression that work for Stylish? I didn't find anything on Google about it

Pierrou
  • 303
  • 3
  • 21

3 Answers3

0

The \w{2,6} does not match website because that contains 7 characters. 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.

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.

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)://
  • (?:\w+\.)* Match 0+ times 1+ word chars followed by a dot
  • website\.com Match website.com
  • (?!\/team|\/forum) Negative lookahead to assert what is directly on the right is not /team or /forum
  • \S* Match 0+ times a non whitespace character

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Ok it seems working but it's weird due subdomains. How to make it work for only one domain? This is the regex demo with links I have : https://regex101.com/r/tA0sV0/38 – Pierrou Apr 07 '19 at 09:36
  • @Pierrou Do you mean like this? https://regex101.com/r/tA0sV0/39 Right now it matches `www\w*` followed by 0+ times a word character. – The fourth bird Apr 07 '19 at 09:38
  • No I mean only for the domain "website.com" not all domains. I use this regex for Stylish (addon on Chrome for Custom CSS) I don't want my CSS to be applied on any website I go on ;) And on you example, if you put something else than www or www2 it doesn't match anymore. As I said on my question, it has to work on any subdomain (www, www2, site, web, app, ...) – Pierrou Apr 07 '19 at 09:42
  • Your comment on the question seemed to work nice for any subdomains! I didn't see it. But how about just the domain "website.com"? – Pierrou Apr 07 '19 at 09:43
  • @Pierrou Now it will match only website.com https://regex101.com/r/RiPq3k/1 – The fourth bird Apr 07 '19 at 09:44
  • It works nice! Do you know if this regex will work in Stylish? – Pierrou Apr 07 '19 at 09:52
  • 1
    I found [this page](https://github.com/stylish-userstyles/stylish/wiki/Applying-styles-to-specific-sites#advanced-matching-with-regular-expressions) with the guidelines about matching with a regular expression, so I think you have to modify it to those guidelines. – The fourth bird Apr 07 '19 at 09:58
0

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
  2. https://www.website.com = allowed
  3. http://www.website.com = allowed https://www.website.com/team = not allowed
  4. https://www.website.com/forum = not allowed
  5. https://www.website.com/samplepage = allowed
0

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/'));