How to detect emojis in string

As the title implies, I am attempting to check if emojis exist in a string, check if there are too many, and if the emoji is whitelisted.
I have yet to find a conclusive way to detect emojis in a string.
I’ve tried to detect non-Latin characters, but that just resulted in Russian being flagged as an emoji, which it is definitely not.

local function isEmoji(cp)
	return (cp >= 0x2600 and cp <= 0x27FF)
		or (cp >= 0x1F000 and cp <= 0x1F6FF)
end

local function filterWhitelistedEmojis(text)
	local count = 0
	for _, cp in utf8.codes(text) do
		if isEmoji(cp) then
			local char = utf8.char(cp)
			if not table.find(whitelistedEmojis, char) then
				return false, char -- allow use of certain emojis (🔥, 🙏, etc) 
			end
			count += 1
			if count > emojilimit then
				return false, "too many emojis"
			end
		end
	end
	return true
end

Any help is appreciated, thanks!

How this works is that it checks for emoji unicodes

In case anybody is wondering, here is my translated code.

local function isEmoji(cp)
	if cp >= 0x1F300 and cp <= 0x1F5FF then
		return true
	end

	if cp >= 0x2500 and cp <= 0x2BEF then
		return true
	end

	if cp >= 0x1F600 and cp <= 0x1F64F then
		return true
	end

	if cp >= 0x2702 and cp <= 0x27B0 then
		return true
	end

	return false
end

isEmoji used with Russian, and Latin characters do not trigger, while actual emojis do!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.