Social
Discord invite link Regex
Discord invite links let people join a server with one click. They come as short discord.gg links or longer discord.com/invite links, and older ones may use discordapp.com. The invite code is either a random string or a vanity name made of letters, digits and hyphens. This pattern accepts all of those forms with optional https and www. It is useful for community directories, moderation bots that flag invites in chat and forms that collect a server link. It cannot tell whether an invite has expired or reached its use limit, which only Discord itself knows.
Pattern (JavaScript)
/^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$/Starts at the start of the line/string, then (the text "http", then the character "s", optionally (zero or one time), then the text "://"), optionally (zero or one time), then (the text "www."), optionally (zero or one time), then (the text "discord.gg" OR the text "discord", then (the text "app"), optionally (zero or one time), then the text ".com/invite"), then the character "/", then one of the characters A-Za-z0-9-, between 2 and 32 times, then the character "/", optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ https://discord.gg/TestInv1
- ✓ discord.com/invite/regex-lab
- ✓ https://discordapp.com/invite/abc123
Does not match
- ✗ https://discord.com/channels/123/456
- ✗ https://discord.gg/
- ✗ https://example.gg/TestInv1
- ✗ discord.gg/bad code
Try it
0 matches (JavaScript engine)
https://discord.gg/TestInv1
discord.com/invite/regex-lab
https://discordapp.com/invite/abc123
https://discord.com/channels/123/456
https://discord.gg/
https://example.gg/TestInv1
discord.gg/bad codeCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:https?:\/\/)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com\/invite)\/[A-Za-z0-9-]{2,32}\/?$/g;
const text = "https://discord.gg/TestInv1";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$')
text = 'https://discord.gg/TestInv1'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$#';
$text = 'https://discord.gg/TestInv1';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:https?://)?(?:www\\.)?(?:discord\\.gg|discord(?:app)?\\.com/invite)/[A-Za-z0-9-]{2,32}/?$");
String text = "https://discord.gg/TestInv1";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$`)
text := "https://discord.gg/TestInv1"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$");
var text = "https://discord.gg/TestInv1";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$").unwrap();
let text = "https://discord.gg/TestInv1";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:https?:\/\/)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com\/invite)\/[A-Za-z0-9-]{2,32}\/?$/
text = 'https://discord.gg/TestInv1'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Can I use this to block invite spam in chat?
- Yes. Remove the ^ and $ anchors and add the g flag so it finds invites inside messages. Many moderation bots use a pattern like this to delete or hold messages containing invites.
- What is a vanity invite?
- Boosted servers can choose a readable invite code such as regex-lab instead of a random one. Vanity codes may include hyphens, which is why the character class allows them.
- Why are channel links rejected?
- Links under discord.com/channels open a specific channel for existing members and do not let new people join. Only invite links grant access to a server.
Related patterns
- Facebook page URL^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$
- GitHub repository URL^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$
- LinkedIn profile URL^(?:https?://)?(?:[a-z]{2,3}\.)?linkedin\.com/in/[A-Za-z0-9-]{3,100}/?$
- Subreddit reference (r/name)(?:^|[\s(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b