0

I need a Regex to match all unicode emojis. I need this to check if a discord message has a Unicode Emoji in it.

// Here is a example of matching one Unicode Emoji
message.content.match(/😊/g)
// or two
message.content.match(/πŸ˜‡|😊/g)
// and three
message.content.match(/πŸ˜‡|😊|😈/g)
// then so on.

I have gotten up to like the last 300 or so emojis with this method then it stops working, sending all kinds of errors and stuff.

Is there a better way to do this? and if there is then can you give me an example in the comments. Im new to coding and don't really understand regex and some other stuff so having an example would help a lot. Also I code in JavaScript with node.js on Visual Studio Code.

UPDATE: there is a way that does work and i'm so grateful to Lioness100 for telling about it. There is a npm package called Emoji-Regex. Very useful!

2
  • If your browser supports it, var emojiRegex = /\p{Emoji}/gu. Oct 24, 2020 at 3:49
  • 2
    I believe you're looking for emoji-regex
    – Lioness100
    Oct 24, 2020 at 10:37
1

Regex to match all emojis:

/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g

You can test it here: https://www.regextester.com/106421

0
message.content.match(/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g);

source

0

there is a npm package that can match all unicode emojis! link to the package https://www.npmjs.com/package/emoji-regex and here is some example code!

//this is to import the emoji regex package.
const emojiRegex = require('emoji-regex/RGI_Emoji.js');

//this in the actual command body. 
const regex = emojiRegex();
let a1 = message.content.match(regex)
message.channel.send(a1)

then if you activate the command: -test 🀣

it will return with: 🀣

Very useful!!

1
  • Unfortunately I need to say that emoji-regex doesn't work for all emojis :-( eg. πŸ– πŸ–Š πŸ–Œ πŸ– I've created a codesandbox
    – Kosmonaft
    May 16, 2021 at 22:59

Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.