Skip to content

Instantly share code, notes, and snippets.

@jiyuujin
Created September 16, 2021 23:51
Show Gist options
  • Save jiyuujin/5d75edcb57b233e6e04918c399aecdfe to your computer and use it in GitHub Desktop.
Save jiyuujin/5d75edcb57b233e6e04918c399aecdfe to your computer and use it in GitHub Desktop.
絵文字を考慮して数える
/**
* 絵文字を含む場合の文字数 (サロゲートペアと呼ばれる領域を間引く)
* @param val 対象文字列
*/
export const countCharactersIncludedEmoji = (val: string): number => {
return val?.toString().length - (val?.toString().split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g).length - 1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment