0

I want to convert given Unicode Chars into Emojis. From a function, I get a string and sometimes there are emojis in it but as Unicode (like this \ud83c\uddee\ud83c\uddf9). So I need to check first if this functions contains these Unicode emoji chars and then I need to convert them into emojis.

With this line of code, I tried to remove these Unicode chars, but it doesn't work. But now I need to find a method to convert these Unicode chars into Emojis and not removing them!

var fullnameWOE = fullname[1].replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, '')

EDIT: still found no method to solve this problem... I have to add that I'm getting this string with name containing emoji from a php file, so if there are any opportunities to use php to solve it?

glennsl
  • 28,186
  • 12
  • 57
  • 75
  • These Unicodes already represent emojis, so there is no point in converting them. – Zsigmond Lőrinczy Jun 18 '17 at 16:43
  • Edit: here is a JavaScript example: `` – Zsigmond Lőrinczy Jun 18 '17 at 16:49
  • Yeah you are right, but i get from this function for example the String: "Dan\ud83c\uddee\ud83c\uddf9" and when i'm alerting only "\ud83c\uddee\ud83c\uddf9" then it will get converted to a emoji but this whole string doesnt gets converted. – B. Nellson Jun 18 '17 at 17:18
  • Tried `alert("Dan\ud83c\uddee\ud83c\uddf9")` on jsfiddle.net, it seemed working. (Edit: or Shift+F4 (Scratchpad) in Firefox) – Zsigmond Lőrinczy Jun 18 '17 at 19:06
  • Possible duplicate of [javascript and string manipulation w/ utf-16 surrogate pairs](https://stackoverflow.com/questions/6885879/javascript-and-string-manipulation-w-utf-16-surrogate-pairs) – JosefZ Jun 18 '17 at 19:14
  • Thanks @JosefZ that is looking good. But I have to say that I don't know how to solve my problem. I have this var with a Name and sometimes with emojis but these emojis are just shown as this "\ud83c\uddee\ud83c\uddf9" stuff. So how can I can convert this var to show the EMOJI when displaying it. Thanks – B. Nellson Jun 19 '17 at 14:11

2 Answers2

0

just refere to this one How to find whether a particular string has unicode characters (esp. Double Byte characters) to check if unicodes are present.

But in order to replace with emoticons I suggest you to use a dictionary because it will be more controlled.

const raw = 'This string could contain an emoticon: {{example of emoticon unicode}}';

const EMOJS = {
  'example of emoticon unicode': 'REPLACED WITH CORRESPONDING VALUE'
};

function compile(input, dict = EMOJS) {
  return Object
    .keys(dict)
    .reduce(
      (res, emojId) => {
        let tmp;
        do {
          tmp = res;
          
          res = res.replace(emojId, dict[emojId]);
        } while(tmp !== res);
        
        return res;
      },
      input
    )
}

console.log({input: raw, output: compile(raw)});
Hitmands
  • 13,491
  • 4
  • 34
  • 69
0

here you go:

function containsNonLatinCodepoints(s) {
    return /[^\u0000-\u00ff]/.test(s);
}
David Dehghan
  • 22,159
  • 10
  • 107
  • 95