Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript detect if a string contains only unicode emojis [duplicate]

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

like image 999
Doua Beri Avatar asked Dec 10 '16 19:12

Doua Beri


3 Answers

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;
like image 163
bholben Avatar answered Nov 14 '22 23:11

bholben


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

like image 24
Lucas C. Feijo Avatar answered Nov 15 '22 00:11

Lucas C. Feijo


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;
like image 32
Ilarion Halushka Avatar answered Nov 14 '22 23:11

Ilarion Halushka