0

In [this answer], there's an example on how to exclude a certain word from the search. I'm using it to filter out CSS, JS and GIF calls in the network tab of Chrome.

^((?!badword).)*$

When I play with different combinations to exclude both the string css and gif, I get results that either exclude everything or include everything.

I've tried a bunch of different combinations using the pipe operator to mimic the concept of OR. Neither work and I'm out of ideas.

How should I reason to get it working? It was surprisingly hard to find an example that excluded multiple words.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Do you mean using a negative lookahead with an alternation? `^(?!.*\.(?:gif|css|js)).*$` https://regex101.com/r/yUlOCf/1 – The fourth bird Sep 29 '19 at 10:42
  • @Thefourthbird I fear that I'm asking wrong question. The example of yours is great and does precisely what I described. When I pasted it in the filter box in Chrome on the network tab to remove the calls that targeted those file extensions, I got nothing left in the list, though. – Konrad Viltersten Sep 29 '19 at 18:00
  • Did you try putting it between forward slashes? `/^(?!.*\.(?:gif|css|js)).*$/` – The fourth bird Sep 29 '19 at 18:04
  • @Thefourthbird What a silly mistake. You're perfectly right. This works just as expected: `/^(?!.*\.(?:gif|css|js)).*$/` but I noticed that when I tried to set a dot as additional condition, just in case, the result was that the match failed. I tried to escape the dot like this: `/^(?!.*\.(?:\.gif|\.css|\.js)).*$/` and to have it verbatim like this: `/^(?!.*\.(?:[.]gif|[.]css|[.]js)).*$/`. I suspect it's due to the negative look-ahead that I hardly understand. Got a hint for that too or should I ask a new question? – Konrad Viltersten Sep 29 '19 at 20:00
  • In the pattern `/^(?!.*\.(?:gif|css|js)).*$/` the dot is already part of the condition in this part `\.(?:gif|css|js)` which means match a dot followed by any of the options which are listed using an `|` alternation. If you add a dot as you tried you are matching 2 dots like `..gif`. – The fourth bird Sep 29 '19 at 20:10
  • @Thefourthbird I didn't notice that, despite looking. I believe it's time to drop the keyboard and start hugging a pillow. At any rate - you might want to post your comments as an answer to be accepted. You've been of great help. Thank you. – Konrad Viltersten Sep 29 '19 at 20:37

0 Answers0