Skip to main content

Useful Regexes

Introduction

Sapphire provides a lot of Regexes that you can use in your code

note

If you don't know what a Regex is check the Docs

Regex Cheatsheet

Regex is a topic that's often not easy to understand so here is a cheat sheet to help you understand it. We list many of the common RegEx tokens available to create regular expressions.

  • ^ Matches the beginning of the string, or the beginning of a line if the multiline flag (/m) is used
  • $ Matches the end of the string, or the end of a line if the multiline flag (/m) is used
  • \b Matches the empty string, or a word boundary
  • \B Matches the empty string, or a non-word boundary
  • \d Matches any decimal digit
  • \D Matches any non-digit
  • \f Matches a form feed
  • \n Matches a newline
  • \r Matches a carriage return
  • \s Matches any whitespace character
  • \S Matches any non-whitespace character
  • \t Matches a tab
  • \v Matches a vertical tab
  • . Matches any character except newline
  • \ Matches a backslash
  • [] Matches any character in the brackets
  • [^] Matches any character not in the brackets
  • \w Matches any word character
  • \W Matches any non-word character
  • ? Matches zero or one occurrences of the preceding expression
  • * Matches zero or more occurrences of the preceding expression
  • + Matches one or more occurrences of the preceding expression
  • {n} Matches exactly n occurrences of the preceding expression
  • {n,} Matches n or more occurrences of the preceding expression
  • {n,m} Matches at least n and at most m occurrences of the preceding expression

Usage

To use these useful regexes you need to install @sapphire/discord-utilties first

npm install @sapphire/discord-utilities

Channel Mention Regex

This regex will match any mention of a channel and return the id of the channel

Raw: /^<#(?<id>\d{17,19})>$/

Channel Message Regex

This regex can capture the channel and message IDs in a channelId-messageId pattern This pattern can be found when you hold Shift and hover over a message, and click the "ID" button

Raw: /^(?<channelId>\d{17,19})-(?<messageId>\d{17,19})$/

Discord Hostname Regex

This regex captures every known discord hostname link

Raw: /(?<subdomain>\w+)\.?(?<hostname>dis(?:cord)?(?:app|merch|status)?)\.(?<tld>com|g(?:d|g|ift)|(?:de(?:sign|v))|media|new|store|net)/i

This regex captures discord invite links

Raw: /^(?:https?:\/\/)?(?:www\.)?(?:discord\.gg\/|discord(?:app)?\.com\/invite\/)?(?<code>[\w\d-]{2,})$/i

Emoji Regex

This regex captures every animated and non-animated emoji's id

Raw: /^(?:<(?<animated>a)?:(?<name>\w{2,32}):)?(?<id>\d{17,21})>?$/

Formatted Custom Emoji Regex

This regex matches any animated or non-animated custom Discord emoji. Unlike EmojiRegex It can be a substring of a larger string.

Raw: /<a?:\w{2,32}:\d{17,18}>/

Formatted Custom Emoji With Groups

Regex that can capture any animated or non-animated custom Discord emoji. Similar to FormattedCustomEmoji and unlike EmojiRegex can also be a substring of a larger string.

Raw: /(?<animated>a?):(?<name>[^:]+):(?<id>\d{17,19})/

HTTP Regex

This regex matches any url starting with http or https

Raw: /^https?:\/\//

This regex can capture the Guild, Channel, and Message ID based on a shareable Discord message link.

Raw: /^(?:https:\/\/)?(?:ptb\.|canary\.)?discord(?:app)?\.com\/channels\/(?<guildId>(?:\d{17,19}|@me))\/(?<channelId>\d{17,19})\/(?<messageId>\d{17,19})$/

Parsed CustomEmoji Regex

This regex matches any animated or non-animated custom Discord emoji without the wrapping <...> symbols. This means that a string that matches this regex can directly be send inside a Discord message. Other than this difference it is similar to FormattedCustomEmoji.

Raw: /a?:\w{2,32}:\d{17,18}/

Parsed CustomEmojiWithGroups Regex

Regex that matches any animated or non-animated custom Discord emoji without the wrapping <...> symbols. This means that a string that matches this regex can directly be send inside a Discord message. Other than this difference it is similar to FormattedCustomEmojiWithGroups.

Raw: /(?<animated>a?):(?<name>[^:]+):(?<id>\d{17,19})/

RoleMention Regex

This regex can capture the role ID of a role mention

Raw: /^<@&(?<id>\d{17,19})>$/

Snowflake Regex

This regex matches any Discord Snowflake

Raw: /^(?<id>\d{17,19})$/

Twemoji Regex

This regex matches any Twitter Emoji

Raw: See official source code

UserOrMember Mention Regex

This regex captures the ID of a user or member mentioned

Raw: /^<@!?(?<id>\d{17,19})>$/

WebSocketURL Regex

This regex matches any url starting with wss/ws

Raw: /^wss?:\/\//

Webhook Regex

This regex captures a Discord Webhook ID and token from a webhook url

Raw: /(?<url>^https:\/\/(?:(?:canary|ptb).)?discordapp.com\/api\/webhooks\/(?<id>\d+)\/(?<token>[\w-]+)\/?$)/