How can I remove the last emoji of a group of emojis in javascript?

南楼画角 提交于 2021-02-10 04:19:02

问题


Let's say I have this 3 emojis in a string: 😀🎃👪

There are not any spaces or any other character except emojis in the string.

How can I remove the last emoji in javascript?


回答1:


You can do this. It will always remove the last emoji.

function removeEmoji() {
  var emoStringArray = document.getElementById('emoji').innerHTML;
  var lastIndex = emoStringArray.lastIndexOf(" ");
  var stripedEmoStringArray = emoStringArray.substring(0, lastIndex);
  document.getElementById('emoji').innerHTML = stripedEmoStringArray;
}
<p id="emoji">
😀 🎃 👪
</p>

<button onclick="removeEmoji()">Remove</button>



回答2:


I hope this is what you want.

var emoString = "😀 🎃 👪";
emoString = emoString.slice(0, -2);

However, this would work only if you have 3 emojis in total. Hence to achieve a generalised solution, you can use the underscore functions split() and javascript function join() :

var emoString = "😀 🎃 👪";
emoString = _.rest(emoString.split(' ')).join(' ')

Hope this will solve your issue.




回答3:


Ok, here is how I solved it:

function deleteEmoji(emojiStr) {
    let emojisArray = emojiStr.match(/([\uD800-\uDBFF][\uDC00-\uDFFF])/g);
    emojisArray = emojisArray.splice(0, emojisArray.length - 1);
    return emojisArray.join("");
}
let emojitext = "😀🎃👪";
console.log(deleteEmoji(emojitext));



回答4:


I was actually surprised that unicode in this day an age is still not fully supported in browsers. I assume a lot of this is down to windows and it's version of UTF-16.

The OP I believe has found his own solution to the original problem, but I thought there has to be a more generic solution to surrogate pair unicode characters.

Anyway, so my solution is convert the text into a UTF-32 array, these can then be manipulated must easier, using slice etc.

After you have done what you want to the array, just convert back.

Below is an example.

Some of the code I got from -> Is it possible to convert a string containing "high" unicode chars to an array consisting of dec values derived from utf-32 ("real") codes? and http://speakingjs.com/es5/ch24.html

function decodeUnicode(str) {
    const r = [];
    let i = 0;
    while(i < str.length) {
        let chr = str.charCodeAt(i++);
        if(chr >= 0xD800 && chr <= 0xDBFF) {
            var low = str.charCodeAt(i++);
            r.push(0x10000 + 
              ((chr - 0xD800) << 10) | (low - 0xDC00));
        } else {
            r.push(chr);
        }
    }
    return r;
}

function toUTF16(codePoint) {
    const TEN_BITS = parseInt('1111111111', 2);
    if (codePoint <= 0xFFFF) { return codePoint; }
    codePoint -= 0x10000;
    const leadingSurrogate = 0xD800 | (codePoint >> 10);
    const trailingSurrogate = 0xDC00 | (codePoint & TEN_BITS);
    return String.fromCharCode(leadingSurrogate) +
      String.fromCharCode(trailingSurrogate);
}

function encodeUnicode(data) {
  return data.reduce((a, v) => {
    a += toUTF16(v);
    return a;
  },"");
}

var unicode = decodeUnicode("😀🎃👪");
for (let l = 0; l < unicode.length; l ++)
  console.log(encodeUnicode(
    unicode.slice(0, l ? -l : unicode.length)));
    
console.log("pick some random ones");
let str = "";
for (let l = 0; l < 20; l ++) {
  let rnd = Math.trunc(Math.random()*unicode.length);
  str += encodeUnicode(unicode.slice(rnd,rnd+1));
}
console.log(str);



回答5:


The answer below doesn't use any special package and safely removes last emoji

function safeEmojiBackspace(str)
{
  let initialRealCount = fancyCount(str);
  while(fancyCount(str) !== initialRealCount - 1)
  {
      str = str.substring(0,str.length - 1);
  }
  return str;
}
function fancyCount(str){
  const joiner = "\u{200D}";
  const split = str.split(joiner);
  let count = 0;
  for(const s of split){
    //removing the variation selectors
    const num = Array.from(s.split(/[\ufe00-\ufe0f]/).join("")).length;
    count += num;
  }
  //assuming the joiners are used appropriately
  return count / split.length;
}

Sample usage

let str = "something😀🎃👪";
str = safeEmojiBackspace(str);//"something😀🎃"


来源:https://stackoverflow.com/questions/46909766/how-can-i-remove-the-last-emoji-of-a-group-of-emojis-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!