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
3 Answers
Reset to default 2Looks 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
版权声明:本文标题:javascript - Regex Pattern for All Discord Tag Types - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744953541a2634223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论