remove emojis from a string in typescript

One way to remove emojis from a string in Typescript is by using Regex along with Unicode block ranges. Here's a function that uses regex to remove all emojis from a given string:

index.ts
function removeEmojis(str: string): string {
  const emojiRegex = /[\u{1F600}-\u{1F64F}]|[\u{1F300}-\u{1F5FF}]|[\u{1F680}-\u{1F6FF}]|[\u{2600}-\u{26FF}]|[\u{2700}-\u{27BF}]|\u{1F9E0}/gu;
  return str.replace(emojiRegex, '');
}
227 chars
5 lines

In this regex pattern, we're using Unicode block ranges to match all the possible emojis. The u flag is used to enable support for Unicode characters, and the g flag is used to replace all occurrences of emojis in the string.

Here's an example usage:

index.ts
const textWithEmojis = "Hello 😀 World 🌎";
const textWithoutEmojis = removeEmojis(textWithEmojis);
console.log(textWithoutEmojis); // "Hello  World "
151 chars
4 lines

gistlibby LogSnag