Problem
.split('') splits emojis in half.
Even Onur's solutions only works for some emojis, but can't handle more complex languages or combined emojis.
Consider this emoji being ruined:
[..."🏳️🌈"] // returns ["🏳", "️", "", "🌈"] instead of ["🏳️🌈"]
Also consider this Hindi text अनुच्छेद which is split like this:
[..."अनुच्छेद"] // returns ["अ", "न", "ु", "च", "्", "छ", "े", "द"]
but should in fact be split like this:
["अ","नु","च्","छे","द"]
This happens because some of the characters are combining marks (think diacritics/accents in European languages).
Solution
You can use the grapheme-splitter library for this:
It does proper standards-based letter split in all the hundreds of exotic edge-cases - yes, there are that many.
Install:
$ npm install --save grapheme-splitter
Usage:
const splitter = new GraphemeSplitter();
// plain latin alphabet - nothing spectacular
splitter.splitGraphemes("abcd"); // returns ["a", "b", "c", "d"]
// two-char emojis and six-char combined emoji
splitter.splitGraphemes("🌷🎁💩😜👍🏳️🌈"); // returns ["🌷","🎁","💩","😜","👍","🏳️🌈"]
// diacritics as combining marks, 10 JavaScript chars
splitter.splitGraphemes("Ĺo͂řȩm̅"); // returns ["Ĺ","o͂","ř","ȩ","m̅"]
// individual Korean characters (Jamo), 4 JavaScript chars
splitter.splitGraphemes("뎌쉐"); // returns ["뎌","쉐"]
// Hindi text with combining marks, 8 JavaScript chars
splitter.splitGraphemes("अनुच्छेद"); // returns ["अ","नु","च्","छे","द"]
// demonic multiple combining marks, 75 JavaScript chars
splitter.splitGraphemes("Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞"); // returns ["Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍","A̴̵̜̰͔ͫ͗͢","L̠ͨͧͩ͘","G̴̻͈͍͔̹̑͗̎̅͛́","Ǫ̵̹̻̝̳͂̌̌͘","!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞"]
console.log(arr)here and notalert(arr)