js 获取包含emoji的字符串的长度

      let emoji_exp = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;

      function estring(str) {
        const ms = [...str.matchAll(emoji_exp)];
        if (!ms || !ms.length) return str.length;

        let emojiSize = 0;
        for (const m of ms) emojiSize += m.length - 1;
        return str.length - emojiSize;
      }

      console.log(estring("❤️❤️a🎅")); // 4
posted @ 2020-12-26 16:24  Ajanuw  阅读(859)  评论(0编辑  收藏  举报