regex
Description#
Fires when a message's text matches a regular expression you write, checking new messages and edits. You do not need to know regex first: the guide below teaches the few pieces you need, then works real moderation cases from literal text up to a copy-ready pattern.
A regex trigger fires when a message's text matches a pattern you write. You do not need to learn the whole regex language, only the handful of pieces below. Patterns run without backtracking, so the bot refuses a pattern that could lock up while matching, and the engine does not support backreferences or lookaround. Build a pattern up from the literal text you want to catch, then add the pieces you need. Turn on the rule's ignore-case option (or start the pattern with (?i)) when case should not matter.
The pieces you need#
| piece | what it means |
|---|---|
| \. | A literal dot. A backslash makes a special character (like . or /) mean itself. |
| ? | The thing before it is optional (zero or one). |
| + | One or more of the thing before it. |
| * | Zero or more of the thing before it. |
| {6,} | Six or more of the thing before it (any number works). |
| \b | A word boundary, the edge between a word and a space or punctuation. |
| \s | Any whitespace (space, tab, newline). \S is anything that is not whitespace. |
| \w | A word character: a letter, digit, or underscore. |
| [A-Z] | One character from a set. Here, a single capital letter. |
| (a|b) | Either a or b. |
| (?i) | Ignore case for the rest of the pattern (same as the rule's ignore-case option). |
Parameters#
| parameter | required | what it is | notes |
|---|---|---|---|
| pattern | yes | regex pattern | |
| ignoreCase | optional | ignore case |
Permissions#
Configured in the portal by people who manage the server. The bot reads message text through the Message Content intent, which it has enabled.
Worked cases#
Blocking invite links#
Stop people posting Discord invite links.
Start from the text an invite begins with, discord.gg. The dot is special, so escape it to discord\.gg. Then require the invite code after the slash with \S+ (one or more non-space characters).
pattern discord\.gg/\S+ (ignore case: on)
Catches: Invite links like discord.gg/AbC123.
Ignores: The word discord on its own. To also catch the other invite host, add discord(?:app)?\.com/invite/\S+.
Blocking every web link#
Stop any web link.
A link starts with http, then an optional s (s?), then ://, then the rest is non-space: https?://\S+.
pattern https?://\S+ (ignore case: on)
Catches: Any http:// or https:// URL.
Ignores: Plain text with no scheme, like a bare example.com typed without http.
Stopping caps shouting#
Stop all-caps shouting without flagging short acronyms.
A run of capitals is [A-Z]. Require a long run, six or more, with word boundaries so a short acronym in a sentence is left alone: \b[A-Z]{6,}\b.
pattern \b[A-Z]{6,}\b (ignore case: off)
Catches: Six or more capital letters in a row, like SHOUTING.
Ignores: Hello, OK, and normal Sentence Case.
Note: Turn the rule's ignore-case option OFF for this pattern. With ignore-case on, lowercase letters count as capitals and the pattern matches every message.
Catching the free-Nitro scam#
Catch the common free-Nitro scam phrase.
The phrase is free nitro, maybe with extra spaces. Allow any whitespace between the words with \s*, and ignore case with (?i): (?i)free\s*nitro.
pattern (?i)free\s*nitro (ignore case: on)
Catches: free nitro, FREE Nitro, freenitro.
Ignores: The word free or nitro on its own.
Note: This fires on anyone who types the phrase, including a moderator warning others. Pair it with a warn or a threshold, not an instant ban.
Catching leaked bot tokens#
Catch a leaked Discord bot token before it spreads.
A bot token is three chunks of token characters (letters, digits, dash, underscore) joined by dots: a long first chunk, a short middle, a long last. [\w-]{24}\.[\w-]{6}\.[\w-]{27,}.
pattern [\w-]{24}\.[\w-]{6}\.[\w-]{27,} (ignore case: off)
Catches: Strings shaped like a bot token.
Ignores: Ordinary words and plain numbers.
Stopping emoji spam#
Stop walls of custom server emoji.
A custom emoji looks like <:name:12345> (animated ones start <a:). Match one with <a?:\w+:\d+>, then require several in a row, allowing spaces between: (?:<a?:\w+:\d+>\s*){5,}.
pattern (?:<a?:\w+:\d+>\s*){5,} (ignore case: off)
Catches: Five or more custom emoji in a row.
Ignores: A few emoji, or normal text. Note this matches server custom emoji, not plain unicode emoji.
Catching invisible-character evasion#
Catch hidden zero-width characters used to slip words past a filter.
Zero-width and invisible characters have specific code points. Match any one of them in a set: [\u200B\u200C\u200D\u2060\uFEFF] (zero-width space, non-joiner, joiner, word-joiner, and the byte-order mark).
pattern [\u200B\u200C\u200D\u2060\uFEFF] (ignore case: off)
Catches: Any message containing a zero-width or invisible character.
Ignores: Normal visible text.
Note: Invisible characters are not removed automatically. Matching runs against the raw message text, so match them explicitly with this pattern.
Stopping blank messages#
Stop empty or whitespace-only messages (often just an invisible character or an image with no text).
Anchor the whole message with ^ and $, and allow only whitespace and invisible characters between them: ^[\s\u200B\u200C\u200D\u2060\uFEFF]*$.
pattern ^[\s\u200B\u200C\u200D\u2060\uFEFF]*$ (ignore case: off)
Catches: A message that is only spaces, newlines, or invisible characters.
Ignores: Any message with visible text in it.
Blocking bad words#
Block specific words, optionally resisting spaced-out bypasses.
For a plain list of words the word-list trigger is friendlier and needs no regex. Use regex when you want bypass resistance. Simple form, whole words only: (?i)\b(spam|scam)\b. Bypass-resistant, allowing non-word characters between the letters people use to evade filters: (?i)s+\W*c+\W*a+\W*m+.
pattern (?i)\b(spam|scam)\b (ignore case: on)
Catches: The whole word spam or scam (simple form), or s c a m and s-c-a-m (bypass-resistant form).
Ignores: scamper and spammer, because the simple form has word boundaries. The bypass-resistant form is greedier and can over-match, so test it before you rely on it.
Note: Bypass-resistant version: (?i)s+\W*c+\W*a+\W*m+
Where to test#
Try patterns in an interactive tester. Set its flavor to .NET. Read the details in Microsoft's regex reference.
The external tester allows features the bot does not (backreferences and lookaround), so a pattern can pass there and still be rejected when you save. The bot's save-time validation is the final word on what it accepts, and the tester built into the rule builder runs that same check as you type.