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, '');
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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’sRegExpis 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: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.