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

3

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

Nailhead answered 10/12, 2016 at 19:29 Comment(2)
Anchored repeating alternatives should do it: /^(?:alternative1|alternative2|alternative3)*$/.test(str) – Leela
Your code already has that regex. ?? – Alexalexa
A
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;
Archivolt answered 25/5, 2017 at 3:11 Comment(2)
This range does not work for emojis like πŸ§‘β€πŸ¦½πŸ€Ύβ€β™‚οΈ With this range, it works perfectly: #24841167 – Gadabout
This range is incomplete. Use \p{Emoji} instead https://mcmap.net/q/235982/-how-to-detect-emoji-using-javascript – Lenity
S
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

Supergalaxy answered 13/5, 2020 at 19:27 Comment(2)
This range is incomplete. Use \p{Emoji} instead https://mcmap.net/q/235982/-how-to-detect-emoji-using-javascript – Lenity
@NinoFiliu nice – Supergalaxy
N
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;
Neomineomycin answered 7/4, 2019 at 17:16 Comment(1)
This range is incomplete. Use \p{Emoji} instead https://mcmap.net/q/235982/-how-to-detect-emoji-using-javascript – Lenity

© 2022 - 2024 β€” McMap. All rights reserved.