-1

Example picture

Example picture

Hi. I want to test if strings contain only English characters and numbers and emojis.

I know how to test English characters and numbers.

var english = /^[A-Za-z0-9]*$/; english.test("...");

But I don't know how to test emojis... I found some links but I don't know how to use them.

How to remove emoji code using javascript?

https://thekevinscott.com/emojis-in-javascript/

Thanks in advance.

ericobi
  • 529
  • 3
  • 11

3 Answers3

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

supposedly matches all emojis according to https://www.regextester.com/106421

So you can use simply 'or' it with /[A-Za-z0-9]*/:

/^[A-Za-z0-9\u00a9\u00ae\u2000-\u3300\ud83c\ud000-\udfff\ud83d\ud000-\udfff\ud83e\ud000-\udfff]*$/

I tested this with some regex, and it seemed incomplete (matched most emojis but missed a few). Perhaps the list of emojis has grown since the post. But you can adjust the emoji sequence per the links you posted.

https://regex101.com/r/mAA8L0/1

junvar
  • 11,151
  • 2
  • 30
  • 46
0

You can use:

/(?:[A-Za-z0-9]|[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ude3a]|[\ud83c\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g

Example: https://regexr.com/4r4t3 (The emojis I added would not save to the URL, but you can enter them into the tester and see)

Matches every emoji I tried from a wide random selection of the basic set.

Michael Rodriguez
  • 2,142
  • 1
  • 9
  • 15
0
var emoji = /(?:[A-Za-z0-9]|[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ude3a]|[\ud83c\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g;
var teststr = "cute❤️";
       if (emoji.test(teststr)) {
        console.log("ok");
      }
ericobi
  • 529
  • 3
  • 11