I have been working on a project similar to the one showcased on this website. I've managed to successfully convert UTF16 into UTF string using the code snippet below:
function decodeFBEmoji (fbString) {
// Convert String to Array of hex codes
const codeArray = (
fbString
.split('')
.map(char => (
char.charCodeAt(0)
)
);
// Convert plain JavaScript array to Uint8Array
const byteArray = Uint8Array.from(codeArray);
// Decode byte array as a UTF-8 string
return new TextDecoder('utf-8').decode(byteArray);
Now, my next challenge is to extract the Unicode from a text string and replace it with its decoded counterpart to display as an emoji without altering the rest of the text. However, when attempting to use regex for extracting the Unicode string, I encounter issues.
function replaceEmoji(text){
let str = "lorem ipsum lorem ipsum \u00e2\u009d\u00a4\u00ef\u00b8\u008f lorem ipsum";
let res = str.match(/[\\]\w+/g);
console.log(str);
console.log(res);
}