Search code examples
regexblock

Regex to block pattern except in certain situations


I am working with the following regex:

[\s.,_-]*[wW][\s.,_-]*[wW][\s.,_-]*[wW]

It blocks all instances of www in a chat window for me ... there in lies the issue.

I want it to keep looking for spaces and special chars before the first w and still block, but I also want it to leave words like awwwwwww and ewwwwwwww alone completely.

I feel like I'm not far off but I have exhausted my regex know how.


Solution

  • To answer your question, you could use

    \b[wW](?:[\s.,_-]*[wW]){2}\b
    

    \b is a word boundary, (?:...) is a non capturing parenthesis.

    But indeed, without more context it's hard to tell if your URL filter won't break on strange output.