Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all emoji in string to unicode JS

I have a problem with replace emoji in string to string with unicode.

For example:

I have string: const str = "My string ๐Ÿ˜€ is with emoji ๐Ÿ˜€"

I need to convert this string to const str = "My string EMOJI UNICODE is with emoji EMOJI UNICODE"

emoji unicode should look like : [e-1f60e]. Because I have function for converting string with unicode to string with emoji:

function convertEmoji(str) {
  return str.replace(/\[e-([0-9a-fA-F]+)\]/g, (match, hex) =>
    String.fromCodePoint(Number.parseInt(hex, 16))
  );
}

console.log(convertEmoji('string [e-1f60e] sadfsadfsadf'));  // "string ๐Ÿ˜Ž sadfsadfsadf"
like image 523
Anon Avatar asked Oct 20 '25 16:10

Anon


2 Answers

You can use replace as you do in your function going the other way. This answer provides a regex for modern JavaScript that matches various "emoji" ranges. Then in the callback you can use codePointAt to get the value of the code point of the emoji, convert it to hex via toString(16), and return a string in your desired format:

const str = "My string ๐Ÿ˜€ is with emoji ๐Ÿ˜€"
const rex = /[\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}]/ug;
const updated = str.replace(rex, match => `[e-${match.codePointAt(0).toString(16)}]`);
console.log(updated);

See Wiktor's answer as well. ES2018 adds Unicode property escapes. But unfortunately, support is still spotty, though the one he uses in his answer works in Chromium and its derivatives (Chrome, Brave, Chromium Edge) and iOS Safari, though sadly not yet in Firefox.

like image 70
T.J. Crowder Avatar answered Oct 23 '25 07:10

T.J. Crowder


If you target ECMAScript 2018 and newer, you may use

/\p{Emoji}/ug

JS demo:

const str = "My string ๐Ÿ˜€ is with emoji ๐Ÿ˜€";
console.log(
  str.replace(/\p{Emoji}/ug, (m, idx) =>
   `[e-${m.codePointAt(0).toString(16)}]`
  )
);
like image 42
Wiktor Stribiลผew Avatar answered Oct 23 '25 07:10

Wiktor Stribiลผew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!