Search code examples
regexstylish

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)

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


Solution

  • 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