Patterns to contruct reliable regular expressions to match emojis in a string. ππΈπ»
Most solutions try to be clever by using character ranges to guess what should be an emoji, but these ranges are often incomplete and are hard to keep up-to-date as the Unicode emoji list changes over time. This package generates regex patterns using an object map of all real emojis, generated using the information extracted from the Emoji source data.
When new updates are released by Unicode, this library can easily generate a new map object and cut new releases quickly and efficiently.
The tradeoff with this approach is a slightly larger bundle in exchange for reliability.
This package is a fork of tonton-pixel/emoji-test-patterns, which was designed for use in Node. emoji-test
can be used in any JavaScript runtimeβincluding the browser.
- Providing patterns as strings instead of regular expressions does require the extra step of using
new RegExp ()
to actually make use of them, but it has two main advantages:- Flags can be set differently depending on how the patterns are used. In any case, the regular expressions must include the 'u' flag, since the patterns make use of the new type of Unicode escape sequences:
\u{1F4A9}
. - The patterns can be further modified before being turned into regular expressions; for instance, the pattern can be embedded in a larger one (see examples below).
- Flags can be set differently depending on how the patterns are used. In any case, the regular expressions must include the 'u' flag, since the patterns make use of the new type of Unicode escape sequences:
Navigate to your project directory and run
npm install emoji-test
# or
yarn add emoji-test
A basic test can be performed by running the following command from the package directory:
yarn test
import { emojiPatterns } from "emoji-test";
let emojiKeyboardRegex = new RegExp("^" + emojiPatterns.keyboard + "$", "gu");
console.log(emojiKeyboardRegex.test("β€οΈ")); // true!
console.log(emojiKeyboardRegex.test("π·")); // false!
import { emojiPatterns } from "emoji-test";
let emojiAllRegex = new RegExp(emojiPatterns.all, "gu");
console.log(
"AaΔΔ#*0β€π¦ζη±β€οΈμ π π¨π¦π«π·π¬π§π―π΅πΊπΈ πͺβ¬π¨βπ©βπ¦ πβ¬π©ββ€οΈβπ¨ πβ¬π©ββ€οΈβπβπ¨".match(emojiPatterns)
);
// ["β€","β€οΈ","π","π¨π¦","π«π·","π¬π§","π―π΅","πΊπΈ","πͺ","π¨βπ©βπ¦","π","π©ββ€οΈβπ¨","π","π©ββ€οΈβπβπ¨"]
import { emojiPatterns } from "emoji-test";
let emojiAllRegex = new RegExp(emojiPatterns.all, "gu");
let emojiKeyboardRegex = new RegExp("^" + emojiPatterns.keyboard + "$", "u");
let emojiList = "AaΔΔ#*0β€π¦ζη±β€οΈμ π π¨π¦π«π·π¬π§π―π΅πΊπΈ πͺβ¬π¨βπ©βπ¦ πβ¬π©ββ€οΈβπ¨ πβ¬π©ββ€οΈβπβπ¨".match(
emojiAllRegex
);
if (emojiList) {
emojiList = emojiList.filter((emoji) => emojiKeyboardRegex.test(emoji));
}
console.log(emojiList);
// ["β€οΈ","π","π¨π¦","π«π·","π¬π§","π―π΅","πΊπΈ","πͺ","π¨βπ©βπ¦","π","π©ββ€οΈβπ¨","π","π©ββ€οΈβπβπ¨"]
import { emojiPatterns } from "emoji-test";
let emojiAllRegex = new RegExp(emojiPatterns.all, "gu");
console.log(
"AaΔΔ#*0β€π¦ζη±β€οΈμ π π¨π¦π«π·π¬π§π―π΅πΊπΈ πͺβ¬π¨βπ©βπ¦ πβ¬π©ββ€οΈβπ¨ πβ¬π©ββ€οΈβπβπ¨".replace(emojiAllRegex, "")
);
// "AaΔΔ#*0π¦ζη±μ β¬ β¬ β¬"
The MIT License (MIT)