Useful Regexes
Introduction
Sapphire provides a lot of Regexes that you can use in your code
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-utilities first
- npm
- yarn
- pnpm
npm install @sapphire/discord-utilities
yarn add @sapphire/discord-utilities
pnpm add @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,20})>$/
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,20})$/
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
Discord Invite Link Regex
This regex captures discord invite links
Raw
: /(?:^|\b)discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/(?<code>[\w-]{2,255})(?:$|\b)/gi
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,20}>/
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,20})/
HTTP Regex
This regex matches any url starting with http
or https
Raw
: /^https?:\/\//
Message Link Regex
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,20}|@me))\/(?<channelId>\d{17,20})\/(?<messageId>\d{17,20})$/
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,20}/
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,20})/
RoleMention Regex
This regex can capture the role ID of a role mention
Raw
: /^<@&(?<id>\d{17,20})>$/
Snowflake Regex
This regex matches any Discord Snowflake
Raw
: /^(?<id>\d{17,20})$/
Twemoji Regex
This regex matches any Twitter Emoji
UserOrMember Mention Regex
This regex captures the ID of a user or member mentioned
Raw
: /^<@!?(?<id>\d{17,20})>$/
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).)?discord(?:app)?.com\/api(?:\/v\d+)?\/webhooks\/(?<id>\d+)\/(?<token>[\w-]+)\/?$)/