EDIT: as mentioned by vsemozhebuty, the Intl.Segmenter is now (since 2021) build-in, so that should be used.
// default constructor uses system language and segments at grapheme clusters
[...new Intl.Segmenter().segment(
"f๐ฟโบโฃฟ๐งf๐g๐จโ๐ฉโ๐งโ๐ฆll๐ณ๏ธโ๐oอeฬงl^^๐ฅ\n๐จโโค๏ธโ๐โ๐จเค
เคจเฅเคเฅเคเฅเคฆ\rs๐แแ
งแแ
ฐ๐๐ฝa\n\r\ns๐ฉโ๐ปf๐ggZออซออชฬอซฬฝอฬดฬฬคฬออฬฏฬฬ อAอซอฬดอขฬตฬฬฐอLอจองอฉอฬ Gฬอฬฬ
ออฬดฬปอออฬนOอฬฬอฬจฬตฬนฬปฬฬณ!ฬฟฬอฅอฅฬอฃฬฬฬอออฬฌฬฐฬฬ"
)].map(v=>v.segment);
//=> (45)ย ['f', '๐ฟ', 'โบ', 'โฃฟ', '๐ง', 'f', '๐', 'g', '๐จโ๐ฉโ๐งโ๐ฆ', 'l', 'l', '๐ณ๏ธโ๐', 'oอ', 'eฬง', 'l', '^', '^', '๐ฅ', '\n', '๐จโโค๏ธโ๐โ๐จ', 'เค
', 'เคจเฅ', 'เคเฅเคเฅ', 'เคฆ', '\r', 's', '๐', 'แแ
ง', 'แแ
ฐ', '๐๐ฝ', 'a', '\n', '\r\n', 's', '๐ฉโ๐ป', 'f', '๐', 'g', 'g', 'Zออซออชฬอซฬฝอฬดฬฬคฬออฬฏฬฬ อ', 'Aอซอฬดอขฬตฬฬฐอ', 'Lอจองอฉอฬ ', 'Gฬอฬฬ
ออฬดฬปอออฬน', 'Oอฬฬอฬจฬตฬนฬปฬฬณ', '!ฬฟฬอฅอฅฬอฃฬฬฬอออฬฌฬฐฬฬ']
It's definitely not as good as grapheme-splitter (see the answer), but I made a RegExp that works for most combining characters and emojis
/**
* ## RegExp matching each (combined) unicode symbol/character/emoji consecutively
* | flag | description |
* |:----------- |:-------------------------------------------------------------- |
* | G (global) | matches all possible characters |
* | U (unicode) | match with full unicode |
* | Y (sticky) | start matching at beginning of string or end of previous match |
* @type {RegExp}
* @example Array.from("รผ๐ฟ๐๐ฝ^๐จโ๐ฉโ๐งโ๐ฆG".matchAll(unicodeRegExp),v=>v[0]); //=> [ "รผ", "๐ฟ", "๐๐ฝ", "^", "๐จโ๐ฉโ๐งโ๐ฆ", "G" ]
*/
const unicodeRegExp=/(?:\p{Extended_Pictographic}[\p{Emoji_Modifier}\p{M}]*(?:\p{Join_Control}\p{Extended_Pictographic}[\p{Emoji_Modifier}\p{M}]*)*|\s|.)\p{M}*/guy;
(see this RegExp on regex101 for more info on that (click "open regex in editor"))
and a generator function
/**
* ## creates an iterator for each (combined) unicode symbol in the string
* JS generator function yielding each character (with indices)
* @param {string} str - a string
* @param {boolean} [crlf] - if `true` combines `CR` and `LF` into one character `CRLF` (if present in this constellation) - default `false`
* @generator
* @yields {[string,number,number]} `[combined unicode symbol, string index, symbol index]`
* @throws {TypeError} if {@linkcode str} is not a string or {@linkcode crlf} is not a boolean
* @example Array.from(unicodeIterator("รผ๐ฟ๐๐ฝ^๐จโ๐ฉโ๐งโ๐ฆG"),v=>v[0]); //=> [ "รผ", "๐ฟ", "๐๐ฝ", "^", "๐จโ๐ฉโ๐งโ๐ฆ", "G" ]
*/
function*unicodeIterator(str,crlf){
"use strict";
if(typeof str!=="string")throw new TypeError("[unicodeIterator] str is not a string");
if(crlf==null)crlf=false;
else if(typeof crlf!=="boolean")throw new TypeError("[unicodeIterator] crlf is not a boolean");
if(str==="")return;
/**
* ## get UTF 16 unicode symbol (code point) at {@linkcode index} in {@linkcode string}
* @param {string} string - a string
* @param {number} index - zero-based index
* @returns {string} the UTF 16 character at {@linkcode index} (might have length 2, or an empty string if there's nothing to get the code point from)
*/
const utf16At=(string,index)=>{
"use strict";
const codePoint=string.codePointAt(index);
if(codePoint===undefined)return"";
return String.fromCodePoint(codePoint);
}
let l=0;
for(let i=0;i<str.length;){
let symbol="";
do{
const char=utf16At(str,i+symbol.length);
symbol+=char;
if(crlf&&char==="\r"&&str[i+1]==="\n")symbol+="\n";
let zalgo=utf16At(str,i+symbol.length);
while(/^[\p{Emoji_Modifier}\p{M}]$/u.test(zalgo)){
symbol+=zalgo;
zalgo=utf16At(str,i+symbol.length);
}
const joiner=utf16At(str,i+symbol.length);
if(/^\p{Join_Control}$/u.test(joiner))symbol+=joiner;
else break;
}while(true);
yield[symbol,i+=symbol.length,l++];
}
}
I tested both with
f๐ฟโบโฃฟ๐งf๐g๐จโ๐ฉโ๐งโ๐ฆll๐ณ๏ธโ๐oอeฬงl^^๐ฅ\n๐จโโค๏ธโ๐โ๐จเค
เคจเฅเคเฅเคเฅเคฆ\rs๐แแ
งแแ
ฐ๐๐ฝa\n\r\ns๐ฉโ๐ปf๐ggZออซออชฬอซฬฝอฬดฬฬคฬออฬฏฬฬ อAอซอฬดอขฬตฬฬฐอLอจองอฉอฬ Gฬอฬฬ
ออฬดฬปอออฬนOอฬฬอฬจฬตฬนฬปฬฬณ!ฬฟฬอฅอฅฬอฃฬฬฬอออฬฌฬฐฬฬ
(JS string length is 161, and iterating over the code points gets it to 142)
and they both got
49 [ 'f', '๐ฟ', 'โบ', 'โฃฟ', '๐ง', 'f', '๐', 'g', '๐จโ๐ฉโ๐งโ๐ฆ', 'l', 'l', '๐ณ๏ธโ๐', 'oอ', 'eฬง', 'l', '^', '^', '๐ฅ', '\n', '๐จโโค๏ธโ๐โ๐จ', 'เค
', 'เคจเฅ', 'เคเฅ', 'เคเฅ', 'เคฆ', '\r', 's', '๐', 'แ', 'แ
ง', 'แ', 'แ
ฐ', '๐๐ฝ', 'a', '\n', '\r', '\n', 's', '๐ฉโ๐ป', 'f', '๐', 'g', 'g', 'Zออซออชฬอซฬฝอฬดฬฬคฬออฬฏฬฬ อ', 'Aอซอฬดอขฬตฬฬฐอ', 'Lอจองอฉอฬ ', 'Gฬอฬฬ
ออฬดฬปอออฬน', 'Oอฬฬอฬจฬตฬนฬปฬฬณ', '!ฬฟฬอฅอฅฬอฃฬฬฬอออฬฌฬฐฬฬ' ]
which is good enough in my eyes, grapheme-splitter gets you
46 [ 'f', '๐ฟ', 'โบ', 'โฃฟ', '๐ง', 'f', '๐', 'g', '๐จโ๐ฉโ๐งโ๐ฆ', 'l', 'l', '๐ณ๏ธโ๐', 'oอ', 'eฬง', 'l', '^', '^', '๐ฅ', '\n', '๐จโโค๏ธโ๐โ๐จ', 'เค
', 'เคจเฅ', 'เคเฅ', 'เคเฅ', 'เคฆ', '\r', 's', '๐', 'แแ
ง', 'แแ
ฐ', '๐๐ฝ', 'a', '\n', '\r\n', 's', '๐ฉโ๐ป', 'f', '๐', 'g', 'g', 'Zออซออชฬอซฬฝอฬดฬฬคฬออฬฏฬฬ อ', 'Aอซอฬดอขฬตฬฬฐอ', 'Lอจองอฉอฬ ', 'Gฬอฬฬ
ออฬดฬปอออฬน', 'Oอฬฬอฬจฬตฬนฬปฬฬณ', '!ฬฟฬอฅอฅฬอฃฬฬฬอออฬฌฬฐฬฬ' ]
which combines the CR and LF (can be easily added. the generator function already has that as an option) and also splits the แแ
งแแ
ฐ differently (correctly after Default Grapheme Cluster Boundary of UAX #29 of which grapheme-splitter is based on)
Buffer.from(string).lengthBuffer.from(string).lengthreturns 481