skip to Main Content

I need to manually delete an emoji from a string. The problem here is that emojis are presented by multiple unicode symbols, for example:

'🔥'.split('') // output ['uD83D', 'uDD25']

I could check if the previous one is uD83D and delete both, because it seems like every emoji starts with it, but then I found out that some emojis could contain multiple emojis:

'🧑‍💻'.split('') //  ['uD83E', 'uDDD1', '‍', 'uD83D', 'uDCBB']

Though the pattern here is clear, I wonder if there is a best practice for detecting them?

3

Answers


  1. Chosen as BEST ANSWER

    Found a solution based on Intrl.Segmenter(). For those who interested, here is an article


  2. You have to use replace method with your string to replace with empty ”.

    For example :

    yourString.replace(/([u2700-u27BF]|[uE000-uF8FF]|uD83C[uDC00-uDFFF]|uD83E[uDD10-uDDFF]|uD83D[uDC00-uDFFF]|[u2011-u26FF])/g, '');
    

    or

    yourString.replace(/([uE000-uF8FF]|uD83D[uDC00-uDDFF]|uD83C[uDF00-uDFFF])/g, '');
    
    Login or Signup to reply.
  3. This methods works for me

    const removeEmoji = (str) => {
        return str.replace(/[^x00-x7F]+/g, '');
    }
    //Example
    let text = "Hello 👋, World 👍",
    filteredText = removeEmoji(text);
    
    console.log(filteredText); // output: Hello , World 
    

    But it will also remove other non-ascii characters.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search