Skip to content Skip to sidebar Skip to footer

How Can I Remove The Last Emoji Of A Group Of Emojis In Javascript?

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?

Solution 1:

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

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

Sample usage

let str = "somethingπŸ˜€πŸŽƒπŸ‘ͺ";
str = safeEmojiBackspace(str);//"somethingπŸ˜€πŸŽƒ"

Solution 2:

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

functionremoveEmoji() {
  var emoStringArray = document.getElementById('emoji').innerHTML;
  var lastIndex = emoStringArray.lastIndexOf(" ");
  var stripedEmoStringArray = emoStringArray.substring(0, lastIndex);
  document.getElementById('emoji').innerHTML = stripedEmoStringArray;
}
<pid="emoji">
πŸ˜€ πŸŽƒ πŸ‘ͺ
</p><buttononclick="removeEmoji()">Remove</button>

Solution 3:

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.

Solution 4:

Ok, here is how I solved it:

functiondeleteEmoji(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));

Solution 5:

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

functiondecodeUnicode(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;
}

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

functionencodeUnicode(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);

Post a Comment for "How Can I Remove The Last Emoji Of A Group Of Emojis In Javascript?"