Discourse allows the use of regular expressions (regex) in Watched Words.
To use regular expressions (regex) in watched words you must first turn on the
watched words regular expressionssite setting.
If your site is hosted with Discourse, please reach out to email@example.com if you would like to enable this setting.
Regex is extremely powerful and thus dangerous. An incorrectly written regex statement can cause issues for your users. Test your regex statements on non-production instances before going live.
Discourse by default matches all uppercase and lowercase forms of a word entered as a regular expression. That is,
This will match
We could also use a regex expression here:
This will match all of the cases above, plus
This will match
threads but not
However, there’s a glaring error in ALL the above examples! The words
unthreading are matched (
un▪️▪️▪️▪️▪️ing), even though they’re not referring to
thread. How do we fix that?
We’d have to amend our regex to handle word boundaries.
This looks for boundaries around the word so that
threadlike aren’t caught by the filter, but
threads still are.
For handling Unicode characters
This matches all commonly spelled forms of the word grüße — including
Say we want to block the word
Über, but not
Übersicht. Using word boundaries like
Instead we have to make our own boundaries.
This will now appropriately match
ueber, but not
You can also test Regex expressions on https://regex101.com/. If you do so, ensure you switch the regex flavour to ECMAScript.
Regex capture and replace is not supported in Watched Words, only matching, so this will not work on the link or replace actions.