How to remove emoji code using javascript?

后端 未结 12 2187
忘了有多久
忘了有多久 2020-11-27 05:04

How do I remove emoji code using JavaScript? I thought I had taken care of it using the code below, but I still have characters like

12条回答
  •  -上瘾入骨i
    2020-11-27 05:39

    @bobince's solution didn't work for me. Either the Emojis stayed there or they were swapped by a different Emoji.

    This solution did the trick for me:

    var ranges = [
      '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
      '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
      '\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
    ];
    
    
    $('#mybtn').on('click', function() {
      removeInvalidChars();
    })
    
    function removeInvalidChars() {
      var str = $('#myinput').val();
    
      str = str.replace(new RegExp(ranges.join('|'), 'g'), '');
      $("#myinput").val(str);
    }
    
    
    

    Source

提交回复
热议问题