R. Gillie
R. Gillie

Reputation: 1443

Regex Pattern for All Discord Tag Types

doing some discord.js bot development and I'm trying to create a regex that will filter out all types of discord tags (aka tagged users and custom emotes). I've tried a few different things, but none that will capture every case. The 5 main styles of tag are:

<@108012418998792192> (User)
<@!420279649055145996> (User)
<:oof:463391326491377674> (Emote without number in name)
<:Tyler1:311344841466576896> (Emote with number in name)
<:1234:123412314353463456> (Emote that just has number in name)

One of the main issues I'm getting is how diverse the tag types can be. If you notice one user has a @! while another just has @ at the beginning. Emotes are a whole other story with the :ALPHA_NUMERIC: beginnings.

This bot filters quite a lot of messages, so I'm trying to make it as efficient and compact as possible.

I've tried doing things like

arg.replace(/<\D+\d+>/g, '').trim();
arg.replace(/<\D+\w+>/g, '').trim();

But it fails to filter out the last 2.

Upvotes: 2

Views: 8268

Answers (3)

eX C0n
eX C0n

Reputation: 55

Gonna bump this. AKX's answer works for Static Emojis, User Mentions.

Your OP only states those two, but states "filters out all types of discord tags" One simple trick will also cover Role mentions where an ampersand follows the @: <@&ID>, and animated emojis, which follow the following convention: <a:emojiName:ID>

To mitigate Role mentions, Add &? after the !?

To mitigate animated emojis, Add a? before the first colon in the second lookahead

/<((@!?&?\d+)|(a?:.+?:\d+))>/g

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627087

I suggest using

/<(?:[^\d>]+|:[A-Za-z0-9]+:)\w+>/g

See the regex demo. I expnaded the \D shorthand class to make sure it will never overmatch across > right hand boundary.

Details

  • < - a < char
  • (?:[^\d>]+|:[A-Za-z0-9]+:) - either of
    • [^\d>]+ - 1 or more chars other than a digit and >
    • | - or
    • : - a colon,
    • [A-Za-z0-9]+ - 1 or more letters or/and digits
    • : - a colon
  • \w+ - 1 or more letters, digits or _ (replace with [A-Za-z0-9] or [^\W_] to exclude the underscore)
  • > - a > char.

Upvotes: 1

AKX
AKX

Reputation: 169184

Looks like (regexr.com link)

<((@!?\d+)|(:.+?:\d+))>

should do the trick.

Upvotes: 2

Related Questions