How to remove emoji code using javascript?

后端 未结 12 2204
忘了有多久
忘了有多久 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条回答
  •  清歌不尽
    2020-11-27 05:43

    I know this post is a bit old, but I stumbled across this very problem at work and a colleague came up with an interesting idea. Basically instead of stripping emoji character only allow valid characters in. Consulting this ASCII table:

    http://www.asciitable.com/

    A function such as this could only keep legal characters (the range itself dependent on what you are after)

    function (input) {
                var result = '';
                if (input.length == 0)
                    return input;
                for (var indexOfInput = 0, lengthOfInput = input.length; indexOfInput < lengthOfInput; indexOfInput++) {
                    var charAtSpecificIndex = input[indexOfInput].charCodeAt(0);
                    if ((32 <= charAtSpecificIndex) && (charAtSpecificIndex <= 126)) {
                        result += input[indexOfInput];
                    }
                }
                return result;
            };
    

    This should preserve all numbers, letters and special characters of the Alphabet for a situation where you wish to preserve the English alphabet + number + special characters. Hope it helps someone :)

提交回复
热议问题