create regex for emoji in typescript

To create a regex for emoji in TypeScript, we need to understand that emojis are represented by Unicode characters. Unicode defines a range of characters for emojis, and we can use this range to filter out emojis from a string using a regular expression.

Here is an example of how to create a regex for emojis in TypeScript:

index.ts
const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
54 chars
2 lines

This regular expression matches all surrogate pairs in a string, which includes emojis. We can use this regex to filter out emojis from a string or to count the number of emojis in a string. Here is an example:

index.ts
const input = "Hello World! 🌎🌍🌏";
const emojis = input.match(emojiRegex);

console.log(emojis.length); // Output: 3
119 chars
5 lines

This example counts the number of emojis in the string "Hello World! 🌎🌍🌏" and prints the result to the console.

gistlibby LogSnag