最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex Pattern for All Discord Tag Types - Stack Overflow

programmeradmin2浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论