remove emojis from a string in javascript

You can use regex to remove emojis from a string in javascript. Here is an example code snippet:

index.tsx
const removeEmojis = (str) => {
  return str.replace(/[\u{1F600}-\u{1F64F}|\u{1F300}-\u{1F5FF}|\u{1F680}-\u{1F6FF}|\u{2600}-\u{26FF}|\u{2700}-\u{27BF}|\u{1F900}-\u{1F9FF}]/ug, '');
};

// example usage
const textWithEmojis = "I ❤️ coding with emojis 🚀👩‍🚀";
const textWithoutEmojis = removeEmojis(textWithEmojis);
console.log(textWithoutEmojis); // "I  coding with emojis "
376 chars
9 lines

In this code, we are using replace method on the input string with a regex pattern that matches all Unicode characters from several blocks where emojis are defined. The u flag allows matching of Unicode characters, and the g flag makes sure all matches are replaced. The removeEmojis function takes the input string as an argument and returns the string with all the emojis removed.

gistlibby LogSnag