admin管理员组

文章数量:1406950

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 pact 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.

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 pact 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.

Share asked Jul 25, 2018 at 12:56 R. GillieR. Gillie 1,4432 gold badges14 silver badges22 bronze badges 1
  • 1 Try /<(?:\D+|:[A-Za-z0-9]+:)\w+>/g. – Wiktor Stribiżew Commented Jul 25, 2018 at 12:59
Add a ment  | 

3 Answers 3

Reset to default 2

Looks like (regexr. link)

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

should do the trick.

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.

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

本文标签: javascriptRegex Pattern for All Discord Tag TypesStack Overflow