How to remove emoji code using javascript?

manraj82 picture manraj82 · Jun 12, 2012 · Viewed 54k times · Source

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

jony89 picture jony89 · Jan 9, 2017

For me none of the answers completely removed all emojis so I had to do some work myself and this is what i got :

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

Also, it should take into account that if one inserting the string later to the database, replacing with empty string could expose security issue. instead replace with the replacement character U+FFFD, see : http://www.unicode.org/reports/tr36/#Deletion_of_Noncharacters