Search code examples
regexdiscord

Regex expression for a discord username


I cannot seem to find it perfectly, so that's why I'm asking here. What would be the regex expression for the new discord usernames ?

From what I know, they must not contain "@", "#", ":", "```" or "discord". The username cannot be exactly "everyone" nor "here". Whitespaces cannot be put twice in a row, your username cannot start nor end with a whitespace, and I guess that it checks the "only whitespace" case.

I've already tried something, but I cannot really conclude

/^(?!(?:@|```|:|#|discord))$/ // For the "not contain"
/^([^\s].*[^\s])$/ // For the not start/end with whitespace

Solution

  • You can use

    ^(?=.{2,32}$)(?!(?:everyone|here)$)\.?[a-z0-9_]+(?:\.[a-z0-9_]+)*\.?$
    

    See the regex demo.

    Details:

    • ^ - start of string
    • (?=.{2,32}$) - the string must contain two to thirty-two chars
    • (?!(?:everyone|here)$) - the string cannot be equal to "everyone" or "here"
    • \.? - an optional dot
    • [a-z0-9_]+ - one or more lowercase letters/digits/underscores
    • (?:\.[a-z0-9_]+)* - zero or more sequences of a . and then one or more lowercase letters/digits/underscores
    • \.? - an optional dot
    • $ - end of string.