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
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;
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With