Javascript – how to remove emojis from a string

Crossed out emoji, thumbnial for Javascript - how to remove emojis from a string

In this concise article, we will learn how to remove emojis from a string by using the power of regex.

Firstly let’s define a method.

function deleteEmojis (string) {

}

then let’s return the string after running it through the .replace function.

.Replace function will take two variables. First will be your regex or simple string and the second will be a string with which you want the found value to be replaced. Here we are going to use regex of known emoji values and as a second value, we will add an empty string. Thus deleting the Emojis. Simple enough, right?

There are two ways we can do it (regex ways)

1. Old School way to remove Emojis

function deleteEmojis (string) {
  return string.replace(
      /([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,
      ""
    );
}

2. Modern Way

function deleteEmojis (string) {
  return string.replace(/\p{Emoji}/gu, "");
}

I prefer the cleaner modern way as you can actually see what’s going on. Either way, both of these approaches will remove the emojis from a string.

Leave a Comment

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