How to remove emoji code using javascript?

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 🔴.

function removeInvalidChars() {
    return this.replace(/[uE000-uF8FF]/g, '');
}

Answer

The range you have selected is the Private Use Area, containing non-standard characters. Carriers used to encode emoji as different, inconsistent values inside this range.

More recently, the emoji have been given standardised ‘unified’ codepoints. Many of these are outside of the Basic Multilingual Plane, in the block U+1F300–U+1F5FF, including your example 🔴 U+1F534 Large Red Circle.

You could detect these characters with [U0001F300-U0001F5FF] in a regex engine that supported non-BMP characters, but JavaScript’s RegExp is not such a beast. Unfortunately the JS string model is based on UTF-16 code units, so you’d have to work with the UTF-16 surrogates in a regexp:

return this.replace(/([uE000-uF8FF]|uD83C[uDF00-uDFFF]|uD83D[uDC00-uDDFF])/g, '')

However, note that there are other characters in the Basic Multilingual Plane that are used as emoji by phones but which long predate emoji. For example U+2665 is the traditional Heart Suit character ♥, but it may be rendered as an emoji graphic on some devices. It’s up to you whether you treat this as emoji and try to remove it. See this list for more examples.

Leave a Reply

Your email address will not be published. Required fields are marked *