5

I'm using the following function to replace emojis in a string and is working great:

function doEmoji(s){
    var ranges = [
        '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
        '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
        '\ud83d[\ude80-\udeff]'  // U+1F680 to U+1F6FF
    ];
    var x = s.toString(16).replace(new RegExp(ranges.join('|'), 'g'),' whatever ');
    return x;
};

Now I want to check if that string only contains emojis or space characters. The reason why I want to do this is because I want to replace emojis only if no other characters are present(except space).

Some examples:

Hello how are you?  //do nothing
‍‍ // replace emojis
‍‍  // replace emojis

I'm looking for a simple solution, a regex maybe. Thanks

Doua Beri
  • 10,612
  • 18
  • 89
  • 138

3 Answers3

6

Just a minor adjustment to find if the string has only emojis and spaces...

const ranges = [
  '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
  '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
  '\ud83d[\ude80-\udeff]', // U+1F680 to U+1F6FF
  ' ', // Also allow spaces
].join('|');

const removeEmoji = str => str.replace(new RegExp(ranges, 'g'), '');

const isOnlyEmojis = str => !removeEmoji(str).length;
bholben
  • 1,077
  • 1
  • 15
  • 24
  • This range does not work for emojis like ‍‍♂️ With this range, it works perfectly: https://stackoverflow.com/questions/24840667/what-is-the-regex-to-extract-all-the-emojis-from-a-string/42236875#42236875 – Igor O Jan 15 '20 at 12:38
  • This range is incomplete. Use `\p{Emoji}` instead https://stackoverflow.com/a/64007175/8186898 – Nino Filiu Apr 09 '21 at 08:00
3

A simple solution, if you don't want any dependencies:

containsOnlyEmojis(text) {
  const onlyEmojis = text.replace(new RegExp('[\u0000-\u1eeff]', 'g'), '')
  const visibleChars = text.replace(new RegExp('[\n\r\s]+|( )+', 'g'), '')
  return onlyEmojis.length === visibleChars.length
}

It removes all characters from unicode charsets that are regular alphabets and symbols for writing, and leaves out emojis and some other remaining things that, for our use case, was ok, but that's probably the only caveat.

Source for chatset ranges: https://en.wikipedia.org/wiki/Unicode_block

Lucas C. Feijo
  • 1,002
  • 3
  • 13
  • 29
2

In 2018/2019 there are more emoji added, so I modified a bit bholben's RegExp (source: regextester.com):

const ranges = [
    '\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]',
    ' ', // Also allow spaces
].join('|');

const removeEmoji = str => str.replace(new RegExp(ranges, 'g'), '');

const isOnlyEmojis = str => !removeEmoji(str).length;
Ilarion Halushka
  • 2,083
  • 18
  • 13