With the upcoming Intl.Segmenter
. You can do this:
const splitEmoji = (string) => [...new Intl.Segmenter().segment(string)].map(x => x.segment)
splitEmoji("π΄ππβπ ππ") // ['π΄', 'π', 'π', 'β', 'π ', 'π', 'π']
This also solve the problem with "π¨βπ¨βπ§βπ§" and "π¦πΎ".
splitEmoji("π¨βπ¨βπ§βπ§π¦πΎ") // ['π¨βπ¨βπ§βπ§', 'π¦πΎ']
According to CanIUse, apart from IE and Firefox, this is supported by 91.23% of users globally, as of writing.
Until Firefox gets support, as mentioned in Matt Davies' answer, Graphemer is the best solution:
let Graphemer = await import("https://cdn.jsdelivr.net/npm/[email protected]/+esm").then(m => m.default.default);
let splitter = new Graphemer();
let graphemes = splitter.splitGraphemes("π¨βπ¨βπ§βπ§π¦πΎ"); // ['π¨βπ¨βπ§βπ§', 'π¦πΎ']