Updated answer:
The following is based on the below comment from Christopher (which they mention is itself based on a comment from outis).
Original comment from Christopher:
Based on the comment from @outis, here is a small fiddle which makes
use of the regex and information from that answer. And the reference.
Feels like at the moment it is THE ultimate solution.
isEmojiOnly(str) {
const stringToTest = str.replace(/ /g,'');
const emojiRegex = /^(?:(?:\p{RI}\p{RI}|\p{Emoji}(?:\p{Emoji_Modifier}|\u{FE0F}\u{20E3}?|[\u{E0020}-\u{E007E}]+\u{E007F})?(?:\u{200D}\p{Emoji}(?:\p{Emoji_Modifier}|\u{FE0F}\u{20E3}?|[\u{E0020}-\u{E007E}]+\u{E007F})?)*)|[\u{1f900}-\u{1f9ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}])+$/u;
return emojiRegex.test(stringToTest) && Number.isNaN(Number(stringToTest));
}
Old Answer:
The following fails for number emojis and flags and maybe more.
This is what I came up with, not sure how it holds up against all test cases but it should be decent enough to be usable. This function will test the input to see if it ONLY contains emojis.
function isEmojiOnly(str) {
// remove all white spaces from the input
const stringToTest = str.replace(/ /g,'');
const regexForEmojis = /\p{Extended_Pictographic}/ug;
const regexForAlphaNums = /[\p{L}\p{N}]+/ug;
// check to see if the string contains emojis
if (regexForEmojis.test(stringToTest)) {
// check to see if it contains any alphanumerics
if (regexForAlphaNums.test(stringToTest)) {
return false;
}
return true;
}
return false;
}