如何找到unicode组合?

时间:2018-08-27 09:15:44

标签: javascript

我有一些表情符号,例如(‍‍)。我在每个表情符号中都添加了span。我有一个处理代码可以将其转换为span,这里是将转换为span的函数

emojiSpan = function (emojis) {
  if (!emojis)
    return;
  try {
    let emojiSpan;
    var unicodeMatch = /([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g;
    if (emojis.match(unicodeMatch)) {
      emojis.match(unicodeMatch).filter(function (unicode) {
        if (unicode.trim().length === 1) return emojis;
        emojiSpan = _getEmojiSpan(unicode);
        emojis = Utility.replaceAll(emojis, unicode.trim(), emojiSpan);
      });
    }
  } catch (error) {
    console.error("Error while parsing emoji", error.stack);
  }
  return emojis;
};

var _getEmojiSpan = function(emoji){
if(!emoji)
  return;
return `<span class="emojiNative">${emoji}</span>`;  
}

在这里,输入是‍ ‍,我得到的输出是

<span class="emojiNative"></span>‍
<span class="emojiNative">
  <span class="emojiNative"></span>
</span>
<span class="emojiNative"></span>‍
<span class="emojiNative">
  <span class="emojiNative"></span>
</span>
<span class="emojiNative">
  <span class="emojiNative"></span>
</span>
<span class="emojiNative"></span>
<span class="emojiNative">
  <span class="emojiNative"></span>
</span>
<span class="emojiNative"></span>

表情符号分离。如何获得确切的表情符号?请帮助我。

预期输出为

<span class="emojiNative">‍</span>‍
<span class="emojiNative">‍</span>
<span class="emojiNative"></span>
<span class="emojiNative">  </span>
   

1 个答案:

答案 0 :(得分:1)

“合并”为单个符号的表情符号(例如男性/女性程序员)由零宽度连接符U + 200D分隔。您可以扩展到正则表达式,使其看起来像[emoji](?:\u200D[emoji])*,以增加对组合表情符号的支持。

对于旗帜,这些旗帜是通过用两个区域指示器拼写国家代码而制成的。这些指示器处于固定范围内,因此当您找到该范围时,可以检查是否有两个指示器,如果存在,则将它们放在一起:

(?:\uD83C[\uDDE6-\uDDFF]){2}

确保此代码位于正则表达式的其他\uD83C段的之前

emojiSpan = function (emojis) {
  if (!emojis)
    return;
  try {
    var unicodeMatch = /(?:\uD83C[\uDDE6-\uDDFF]){2}|(?:[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])(?:\u200D(?:[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D]))*/g;
    emojis = emojis.replace(unicodeMatch, '<span class="emojiNative">$&</span>');
  } catch (error) {
    console.error("Error while parsing emoji", error.stack);
  }
  return emojis;
};

var _getEmojiSpan = function(emoji){
if(!emoji)
  return;
return `<span class="emojiNative">${emoji}</span>`;  
}
document.body.innerHTML += emojiSpan("‍ ‍  ");