'Discord username format check

How can I check if the username is in the right format?

For example in this format: Test#1234

And not check more than 4 numbers after #.

How can I do that?



Solution 1:[1]

Do you want a regex like this one?

/^((.+?)#\d{4})/

This will match your format and ignore what's following the 4 digits. Change the dot by the characters you want to match.

Solution 2:[2]

Since you have not provided a language I will answer this in JavaScript regex. If I am understanding your problem correctly you're looking to match a string of letters and numbers followed by a hashtag and 4 numbers. We can do that with a very concise expression. \w+ will match one or more letters and numbers and #\d{4} will match a hashtag followed by exactly 4 numbers. Your complete expression will be /\w+#\d{4}/i. I have added the i to the end to ignore case so that the text will match whether it is in caps or not.

const str = 'hello my username is Text#1234.';

console.log(str.match(/\w+#\d{4}/i));

Solution 3:[3]

This is what I came up with

(?!(discordtag|here|everyone)).[^\@\#\:]{2,32}#[\d]{4}

Solution 4:[4]

    /^(?!(here|everyone))^(?!.*(discord|```)).[^\@\#\:]{2,32}#\d{4}$/s

Documentation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SystemGlitch
Solution 2 Nick Abbott
Solution 3
Solution 4