简体   繁体   中英

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?

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>

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.

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

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

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😀🎃"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM