I want to take a string of emoji and do something with the individual characters.
In JavaScript "๐ด๐๐โ๐ ๐๐".length == 13
because "โ"
length is 1, the rest are 2. So we can't do
const string = "๐จโ๐จโ๐งโ๐ง ๐ฆ๐พ ๐ด ๐ ๐ โ ๐ ๐ ๐";
const s = string.split("");
console.log(s);
const a = Array.from(string);
console.log(a);
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, this is supported by all modern browsers.
If you need to support older browsers, as mentioned in Matt Davies' answer, Graphemer is the best solution:
let Graphemer = await import("https://cdn.jsdelivr.net/npm/graphemer@1.4.0/+esm").then(m => m.default.default);
let splitter = new Graphemer();
let graphemes = splitter.splitGraphemes("๐จโ๐จโ๐งโ๐ง๐ฆ๐พ"); // ['๐จโ๐จโ๐งโ๐ง', '๐ฆ๐พ']